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  {
46  VERIFY( !(numext::isfinite)(m(3)) );
47  VERIFY( !(numext::isinf)(m(3)) );
48  VERIFY( (numext::isnan)(m(3)) );
49  VERIFY( !m.allFinite() );
50  VERIFY( m.hasNaN() );
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  {
65  VERIFY( !(numext::isfinite)(m(4)) );
66  VERIFY( (numext::isinf)(m(4)) );
67  VERIFY( !(numext::isnan)(m(4)) );
68  VERIFY( !m.allFinite() );
69  VERIFY( m.hasNaN() );
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  {
83  VERIFY( (numext::isfinite)(m(3)) );
84  VERIFY( !(numext::isinf)(m(3)) );
85  VERIFY( !(numext::isnan)(m(3)) );
86  VERIFY( !m.allFinite() );
87  VERIFY( !m.hasNaN() );
88  }
89 }
90 
91 void 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: Half.h:150
#define isfinite(X)
Definition: main.h:74
#define isinf(X)
Definition: main.h:73
void test_fastmath()
Definition: fastmath.cpp:91
void check(bool b, bool ref)
Definition: fastmath.cpp:12
void check_inf_nan(bool dryrun)
Definition: fastmath.cpp:30
#define VERIFY(a)
Definition: main.h:325
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)
#define isnan(X)
Definition: main.h:72


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:42:03