test_dist_overloads.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright John Maddock 2006.
  2. // Copyright Paul A. Bristow 2007.
  3. // Use, modification and distribution are subject to the
  4. // Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt
  6. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. // test_dist_overloads.cpp
  8. #include <boost/math/concepts/real_concept.hpp> // for real_concept
  9. #include <boost/math/distributions/normal.hpp>
  10. using boost::math::normal_distribution;
  11. #define BOOST_TEST_MAIN
  12. #include <boost/test/unit_test.hpp> // Boost.Test
  13. #include <boost/test/tools/floating_point_comparison.hpp>
  14. #include <iostream>
  15. using std::cout;
  16. using std::endl;
  17. using std::setprecision;
  18. template <class RealType>
  19. void test_spots(RealType)
  20. {
  21. // Basic sanity checks,
  22. // 2 eps as a percentage:
  23. RealType tolerance = boost::math::tools::epsilon<RealType>() * 2 * 100;
  24. cout << "Tolerance for type " << typeid(RealType).name() << " is " << tolerance << " %" << endl;
  25. for(int i = -4; i <= 4; ++i)
  26. {
  27. BOOST_CHECK_CLOSE(
  28. ::boost::math::cdf(normal_distribution<RealType>(), i),
  29. ::boost::math::cdf(normal_distribution<RealType>(), static_cast<RealType>(i)),
  30. tolerance);
  31. BOOST_CHECK_CLOSE(
  32. ::boost::math::pdf(normal_distribution<RealType>(), i),
  33. ::boost::math::pdf(normal_distribution<RealType>(), static_cast<RealType>(i)),
  34. tolerance);
  35. BOOST_CHECK_CLOSE(
  36. ::boost::math::cdf(complement(normal_distribution<RealType>(), i)),
  37. ::boost::math::cdf(complement(normal_distribution<RealType>(), static_cast<RealType>(i))),
  38. tolerance);
  39. BOOST_CHECK_CLOSE(
  40. ::boost::math::hazard(normal_distribution<RealType>(), i),
  41. ::boost::math::hazard(normal_distribution<RealType>(), static_cast<RealType>(i)),
  42. tolerance);
  43. BOOST_CHECK_CLOSE(
  44. ::boost::math::chf(normal_distribution<RealType>(), i),
  45. ::boost::math::chf(normal_distribution<RealType>(), static_cast<RealType>(i)),
  46. tolerance);
  47. }
  48. for(float f = 0.01f; f < 1; f += 0.01f)
  49. {
  50. BOOST_CHECK_CLOSE(
  51. ::boost::math::quantile(normal_distribution<RealType>(), f),
  52. ::boost::math::quantile(normal_distribution<RealType>(), static_cast<RealType>(f)),
  53. tolerance);
  54. BOOST_CHECK_CLOSE(
  55. ::boost::math::quantile(complement(normal_distribution<RealType>(), f)),
  56. ::boost::math::quantile(complement(normal_distribution<RealType>(), static_cast<RealType>(f))),
  57. tolerance);
  58. }
  59. } // template <class RealType>void test_spots(RealType)
  60. BOOST_AUTO_TEST_CASE( test_main )
  61. {
  62. // Basic sanity-check spot values.
  63. // (Parameter value, arbitrarily zero, only communicates the floating point type).
  64. test_spots(0.0F); // Test float. OK at decdigits = 0 tolerance = 0.0001 %
  65. test_spots(0.0); // Test double. OK at decdigits 7, tolerance = 1e07 %
  66. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  67. test_spots(0.0L); // Test long double.
  68. #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582))
  69. test_spots(boost::math::concepts::real_concept(0.)); // Test real concept.
  70. #endif
  71. #else
  72. std::cout << "<note>The long double tests have been disabled on this platform "
  73. "either because the long double overloads of the usual math functions are "
  74. "not available at all, or because they are too inaccurate for these tests "
  75. "to pass.</note>" << std::endl;
  76. #endif
  77. } // BOOST_AUTO_TEST_CASE( test_main )
  78. /*
  79. Output:
  80. Running 1 test case...
  81. Tolerance for type float is 2.38419e-005 %
  82. Tolerance for type double is 4.44089e-014 %
  83. Tolerance for type long double is 4.44089e-014 %
  84. Tolerance for type class boost::math::concepts::real_concept is 4.44089e-014 %
  85. *** No errors detected
  86. */