fastmath.cpp
Go to the documentation of this file.
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) {
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  {
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;
51  }
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  {
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;
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";
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";
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  {
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;
88  }
89 }
90 
91 EIGEN_DECLARE_TEST(fastmath) {
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 }
Matrix3f m
Scalar * b
Definition: benchVecAdd.cpp:17
#define min(a, b)
Definition: datatypes.h:19
Definition: BFloat16.h:88
#define isfinite(X)
Definition: main.h:95
#define isinf(X)
Definition: main.h:94
Reference counting helper.
Definition: object.h:67
void check(bool b, bool ref)
Definition: fastmath.cpp:12
EIGEN_DECLARE_TEST(fastmath)
Definition: fastmath.cpp:91
void check_inf_nan(bool dryrun)
Definition: fastmath.cpp:30
#define VERIFY(a)
Definition: main.h:380
static int g_test_level
Definition: main.h:168
The matrix class, also used for vectors and row-vectors.
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > & setRandom(Index size)
Definition: Random.h:151
#define isnan(X)
Definition: main.h:93


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:34:13