Miao Wang | 2b8756b | 2017-03-06 13:45:08 -0800 | [diff] [blame] | 1 | // This file is part of Eigen, a lightweight C++ template library |
| 2 | // for linear algebra. |
| 3 | // |
| 4 | // Copyright (C) 2015 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 | void check(bool b, bool ref) |
| 13 | { |
| 14 | std::cout << b; |
| 15 | if(b==ref) |
| 16 | std::cout << " OK "; |
| 17 | else |
| 18 | std::cout << " BAD "; |
| 19 | } |
| 20 | |
| 21 | #if EIGEN_COMP_MSVC && EIGEN_COMP_MSVC < 1800 |
| 22 | namespace std { |
| 23 | template<typename T> bool (isfinite)(T x) { return _finite(x); } |
| 24 | template<typename T> bool (isnan)(T x) { return _isnan(x); } |
| 25 | template<typename T> bool (isinf)(T x) { return _fpclass(x)==_FPCLASS_NINF || _fpclass(x)==_FPCLASS_PINF; } |
| 26 | } |
| 27 | #endif |
| 28 | |
| 29 | template<typename T> |
| 30 | void check_inf_nan(bool dryrun) { |
| 31 | Matrix<T,Dynamic,1> m(10); |
| 32 | m.setRandom(); |
| 33 | m(3) = std::numeric_limits<T>::quiet_NaN(); |
| 34 | |
| 35 | if(dryrun) |
| 36 | { |
| 37 | std::cout << "std::isfinite(" << m(3) << ") = "; check((std::isfinite)(m(3)),false); std::cout << " ; numext::isfinite = "; check((numext::isfinite)(m(3)), false); std::cout << "\n"; |
| 38 | std::cout << "std::isinf(" << m(3) << ") = "; check((std::isinf)(m(3)),false); std::cout << " ; numext::isinf = "; check((numext::isinf)(m(3)), false); std::cout << "\n"; |
| 39 | std::cout << "std::isnan(" << m(3) << ") = "; check((std::isnan)(m(3)),true); std::cout << " ; numext::isnan = "; check((numext::isnan)(m(3)), true); std::cout << "\n"; |
| 40 | std::cout << "allFinite: "; check(m.allFinite(), 0); std::cout << "\n"; |
| 41 | std::cout << "hasNaN: "; check(m.hasNaN(), 1); std::cout << "\n"; |
| 42 | std::cout << "\n"; |
| 43 | } |
| 44 | else |
| 45 | { |
Yi Kong | 2aab794 | 2022-02-25 16:32:14 +0800 | [diff] [blame] | 46 | if( (std::isfinite)(m(3))) g_test_level=1; VERIFY( !(numext::isfinite)(m(3)) ); g_test_level=0; |
| 47 | if( (std::isinf) (m(3))) g_test_level=1; VERIFY( !(numext::isinf)(m(3)) ); g_test_level=0; |
| 48 | if(!(std::isnan) (m(3))) g_test_level=1; VERIFY( (numext::isnan)(m(3)) ); g_test_level=0; |
| 49 | if( (std::isfinite)(m(3))) g_test_level=1; VERIFY( !m.allFinite() ); g_test_level=0; |
| 50 | if(!(std::isnan) (m(3))) g_test_level=1; VERIFY( m.hasNaN() ); g_test_level=0; |
Miao Wang | 2b8756b | 2017-03-06 13:45:08 -0800 | [diff] [blame] | 51 | } |
| 52 | T hidden_zero = (std::numeric_limits<T>::min)()*(std::numeric_limits<T>::min)(); |
| 53 | m(4) /= hidden_zero; |
| 54 | if(dryrun) |
| 55 | { |
| 56 | std::cout << "std::isfinite(" << m(4) << ") = "; check((std::isfinite)(m(4)),false); std::cout << " ; numext::isfinite = "; check((numext::isfinite)(m(4)), false); std::cout << "\n"; |
| 57 | std::cout << "std::isinf(" << m(4) << ") = "; check((std::isinf)(m(4)),true); std::cout << " ; numext::isinf = "; check((numext::isinf)(m(4)), true); std::cout << "\n"; |
| 58 | std::cout << "std::isnan(" << m(4) << ") = "; check((std::isnan)(m(4)),false); std::cout << " ; numext::isnan = "; check((numext::isnan)(m(4)), false); std::cout << "\n"; |
| 59 | std::cout << "allFinite: "; check(m.allFinite(), 0); std::cout << "\n"; |
| 60 | std::cout << "hasNaN: "; check(m.hasNaN(), 1); std::cout << "\n"; |
| 61 | std::cout << "\n"; |
| 62 | } |
| 63 | else |
| 64 | { |
Yi Kong | 2aab794 | 2022-02-25 16:32:14 +0800 | [diff] [blame] | 65 | if( (std::isfinite)(m(3))) g_test_level=1; VERIFY( !(numext::isfinite)(m(4)) ); g_test_level=0; |
| 66 | if(!(std::isinf) (m(3))) g_test_level=1; VERIFY( (numext::isinf)(m(4)) ); g_test_level=0; |
| 67 | if( (std::isnan) (m(3))) g_test_level=1; VERIFY( !(numext::isnan)(m(4)) ); g_test_level=0; |
| 68 | if( (std::isfinite)(m(3))) g_test_level=1; VERIFY( !m.allFinite() ); g_test_level=0; |
| 69 | if(!(std::isnan) (m(3))) g_test_level=1; VERIFY( m.hasNaN() ); g_test_level=0; |
Miao Wang | 2b8756b | 2017-03-06 13:45:08 -0800 | [diff] [blame] | 70 | } |
| 71 | m(3) = 0; |
| 72 | if(dryrun) |
| 73 | { |
| 74 | std::cout << "std::isfinite(" << m(3) << ") = "; check((std::isfinite)(m(3)),true); std::cout << " ; numext::isfinite = "; check((numext::isfinite)(m(3)), true); std::cout << "\n"; |
Yi Kong | 2aab794 | 2022-02-25 16:32:14 +0800 | [diff] [blame] | 75 | std::cout << "std::isinf(" << m(3) << ") = "; check((std::isinf)(m(3)),false); std::cout << " ; numext::isinf = "; check((numext::isinf)(m(3)), false); std::cout << "\n"; |
| 76 | std::cout << "std::isnan(" << m(3) << ") = "; check((std::isnan)(m(3)),false); std::cout << " ; numext::isnan = "; check((numext::isnan)(m(3)), false); std::cout << "\n"; |
Miao Wang | 2b8756b | 2017-03-06 13:45:08 -0800 | [diff] [blame] | 77 | std::cout << "allFinite: "; check(m.allFinite(), 0); std::cout << "\n"; |
| 78 | std::cout << "hasNaN: "; check(m.hasNaN(), 0); std::cout << "\n"; |
| 79 | std::cout << "\n\n"; |
| 80 | } |
| 81 | else |
| 82 | { |
Yi Kong | 2aab794 | 2022-02-25 16:32:14 +0800 | [diff] [blame] | 83 | if(!(std::isfinite)(m(3))) g_test_level=1; VERIFY( (numext::isfinite)(m(3)) ); g_test_level=0; |
| 84 | if( (std::isinf) (m(3))) g_test_level=1; VERIFY( !(numext::isinf)(m(3)) ); g_test_level=0; |
| 85 | if( (std::isnan) (m(3))) g_test_level=1; VERIFY( !(numext::isnan)(m(3)) ); g_test_level=0; |
| 86 | if( (std::isfinite)(m(3))) g_test_level=1; VERIFY( !m.allFinite() ); g_test_level=0; |
| 87 | if( (std::isnan) (m(3))) g_test_level=1; VERIFY( !m.hasNaN() ); g_test_level=0; |
Miao Wang | 2b8756b | 2017-03-06 13:45:08 -0800 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
Yi Kong | 2aab794 | 2022-02-25 16:32:14 +0800 | [diff] [blame] | 91 | EIGEN_DECLARE_TEST(fastmath) { |
Miao Wang | 2b8756b | 2017-03-06 13:45:08 -0800 | [diff] [blame] | 92 | std::cout << "*** float *** \n\n"; check_inf_nan<float>(true); |
| 93 | std::cout << "*** double ***\n\n"; check_inf_nan<double>(true); |
| 94 | std::cout << "*** long double *** \n\n"; check_inf_nan<long double>(true); |
| 95 | |
| 96 | check_inf_nan<float>(false); |
| 97 | check_inf_nan<double>(false); |
| 98 | check_inf_nan<long double>(false); |
| 99 | } |