1 // This file is part of Eigen, a lightweight C++ template library 2 // for linear algebra. 3 // 4 // Copyright (C) 2017 Gael Guennebaud <gael.guennebaud@inria.fr> 5 // 6 // This Source Code Form is subject to the terms of the Mozilla 7 // Public License v. 2.0. If a copy of the MPL was not distributed 8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 9 10 #include "main.h" 11 12 template<typename T> check_abs()13void check_abs() { 14 typedef typename NumTraits<T>::Real Real; 15 16 if(NumTraits<T>::IsSigned) 17 VERIFY_IS_EQUAL(numext::abs(-T(1)), T(1)); 18 VERIFY_IS_EQUAL(numext::abs(T(0)), T(0)); 19 VERIFY_IS_EQUAL(numext::abs(T(1)), T(1)); 20 21 for(int k=0; k<g_repeat*100; ++k) 22 { 23 T x = internal::random<T>(); 24 if(!internal::is_same<T,bool>::value) 25 x = x/Real(2); 26 if(NumTraits<T>::IsSigned) 27 { 28 VERIFY_IS_EQUAL(numext::abs(x), numext::abs(-x)); 29 VERIFY( numext::abs(-x) >= Real(0)); 30 } 31 VERIFY( numext::abs(x) >= Real(0)); 32 VERIFY_IS_APPROX( numext::abs2(x), numext::abs2(numext::abs(x)) ); 33 } 34 } 35 test_numext()36void test_numext() { 37 CALL_SUBTEST( check_abs<bool>() ); 38 CALL_SUBTEST( check_abs<signed char>() ); 39 CALL_SUBTEST( check_abs<unsigned char>() ); 40 CALL_SUBTEST( check_abs<short>() ); 41 CALL_SUBTEST( check_abs<unsigned short>() ); 42 CALL_SUBTEST( check_abs<int>() ); 43 CALL_SUBTEST( check_abs<unsigned int>() ); 44 CALL_SUBTEST( check_abs<long>() ); 45 CALL_SUBTEST( check_abs<unsigned long>() ); 46 CALL_SUBTEST( check_abs<half>() ); 47 CALL_SUBTEST( check_abs<float>() ); 48 CALL_SUBTEST( check_abs<double>() ); 49 CALL_SUBTEST( check_abs<long double>() ); 50 51 CALL_SUBTEST( check_abs<std::complex<float> >() ); 52 CALL_SUBTEST( check_abs<std::complex<double> >() ); 53 } 54