You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
4.1 KiB

  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. #include "main.h"
  10. void check(bool b, bool ref)
  11. {
  12. std::cout << b;
  13. if(b==ref)
  14. std::cout << " OK ";
  15. else
  16. std::cout << " BAD ";
  17. }
  18. #if EIGEN_COMP_MSVC && EIGEN_COMP_MSVC < 1800
  19. namespace std {
  20. template<typename T> bool (isfinite)(T x) { return _finite(x); }
  21. template<typename T> bool (isnan)(T x) { return _isnan(x); }
  22. template<typename T> bool (isinf)(T x) { return _fpclass(x)==_FPCLASS_NINF || _fpclass(x)==_FPCLASS_PINF; }
  23. }
  24. #endif
  25. template<typename T>
  26. void check_inf_nan(bool dryrun) {
  27. Matrix<T,Dynamic,1> m(10);
  28. m.setRandom();
  29. m(3) = std::numeric_limits<T>::quiet_NaN();
  30. if(dryrun)
  31. {
  32. 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";
  33. 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";
  34. 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";
  35. std::cout << "allFinite: "; check(m.allFinite(), 0); std::cout << "\n";
  36. std::cout << "hasNaN: "; check(m.hasNaN(), 1); std::cout << "\n";
  37. std::cout << "\n";
  38. }
  39. else
  40. {
  41. VERIFY( !(numext::isfinite)(m(3)) );
  42. VERIFY( !(numext::isinf)(m(3)) );
  43. VERIFY( (numext::isnan)(m(3)) );
  44. VERIFY( !m.allFinite() );
  45. VERIFY( m.hasNaN() );
  46. }
  47. m(4) /= 0.0;
  48. if(dryrun)
  49. {
  50. 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";
  51. 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";
  52. 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";
  53. std::cout << "allFinite: "; check(m.allFinite(), 0); std::cout << "\n";
  54. std::cout << "hasNaN: "; check(m.hasNaN(), 1); std::cout << "\n";
  55. std::cout << "\n";
  56. }
  57. else
  58. {
  59. VERIFY( !(numext::isfinite)(m(4)) );
  60. VERIFY( (numext::isinf)(m(4)) );
  61. VERIFY( !(numext::isnan)(m(4)) );
  62. VERIFY( !m.allFinite() );
  63. VERIFY( m.hasNaN() );
  64. }
  65. m(3) = 0;
  66. if(dryrun)
  67. {
  68. 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";
  69. 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";
  70. 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";
  71. std::cout << "allFinite: "; check(m.allFinite(), 0); std::cout << "\n";
  72. std::cout << "hasNaN: "; check(m.hasNaN(), 0); std::cout << "\n";
  73. std::cout << "\n\n";
  74. }
  75. else
  76. {
  77. VERIFY( (numext::isfinite)(m(3)) );
  78. VERIFY( !(numext::isinf)(m(3)) );
  79. VERIFY( !(numext::isnan)(m(3)) );
  80. VERIFY( !m.allFinite() );
  81. VERIFY( !m.hasNaN() );
  82. }
  83. }
  84. void test_fastmath() {
  85. std::cout << "*** float *** \n\n"; check_inf_nan<float>(true);
  86. std::cout << "*** double ***\n\n"; check_inf_nan<double>(true);
  87. std::cout << "*** long double *** \n\n"; check_inf_nan<long double>(true);
  88. check_inf_nan<float>(false);
  89. check_inf_nan<double>(false);
  90. check_inf_nan<long double>(false);
  91. }