special_numbers.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2013 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. template<typename Scalar> void special_numbers()
  11. {
  12. typedef Matrix<Scalar, Dynamic,Dynamic> MatType;
  13. int rows = internal::random<int>(1,300);
  14. int cols = internal::random<int>(1,300);
  15. Scalar nan = std::numeric_limits<Scalar>::quiet_NaN();
  16. Scalar inf = std::numeric_limits<Scalar>::infinity();
  17. Scalar s1 = internal::random<Scalar>();
  18. MatType m1 = MatType::Random(rows,cols),
  19. mnan = MatType::Random(rows,cols),
  20. minf = MatType::Random(rows,cols),
  21. mboth = MatType::Random(rows,cols);
  22. int n = internal::random<int>(1,10);
  23. for(int k=0; k<n; ++k)
  24. {
  25. mnan(internal::random<int>(0,rows-1), internal::random<int>(0,cols-1)) = nan;
  26. minf(internal::random<int>(0,rows-1), internal::random<int>(0,cols-1)) = inf;
  27. }
  28. mboth = mnan + minf;
  29. VERIFY(!m1.hasNaN());
  30. VERIFY(m1.allFinite());
  31. VERIFY(mnan.hasNaN());
  32. VERIFY((s1*mnan).hasNaN());
  33. VERIFY(!minf.hasNaN());
  34. VERIFY(!(2*minf).hasNaN());
  35. VERIFY(mboth.hasNaN());
  36. VERIFY(mboth.array().hasNaN());
  37. VERIFY(!mnan.allFinite());
  38. VERIFY(!minf.allFinite());
  39. VERIFY(!(minf-mboth).allFinite());
  40. VERIFY(!mboth.allFinite());
  41. VERIFY(!mboth.array().allFinite());
  42. }
  43. void test_special_numbers()
  44. {
  45. for(int i = 0; i < 10*g_repeat; i++) {
  46. CALL_SUBTEST_1( special_numbers<float>() );
  47. CALL_SUBTEST_1( special_numbers<double>() );
  48. }
  49. }