pi.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /* Boost test/pi.cpp
  2. * test if the pi constant is correctly defined
  3. *
  4. * Copyright 2002-2003 Guillaume Melquiond, Sylvain Pion
  5. *
  6. * Distributed under the Boost Software License, Version 1.0.
  7. * (See accompanying file LICENSE_1_0.txt or
  8. * copy at http://www.boost.org/LICENSE_1_0.txt)
  9. */
  10. #include <boost/numeric/interval.hpp>
  11. #include <boost/limits.hpp>
  12. #include <boost/test/minimal.hpp>
  13. #include "bugs.hpp"
  14. #define PI 3.14159265358979323846
  15. typedef boost::numeric::interval<int> I_i;
  16. typedef boost::numeric::interval<float> I_f;
  17. typedef boost::numeric::interval<double> I_d;
  18. typedef boost::numeric::interval<long double> I_ld;
  19. using boost::numeric::interval_lib::pi;
  20. using boost::numeric::interval_lib::pi_half;
  21. using boost::numeric::interval_lib::pi_twice;
  22. int test_main(int, char *[]) {
  23. I_i pi_i = pi<I_i>();
  24. I_f pi_f = pi<I_f>();
  25. I_d pi_d = pi<I_d>();
  26. I_ld pi_ld = pi<I_ld>();
  27. BOOST_CHECK(in((int) PI, pi_i));
  28. BOOST_CHECK(in((float) PI, pi_f));
  29. BOOST_CHECK(in((double)PI, pi_d));
  30. BOOST_CHECK(subset(pi_i, widen(I_i((int) PI), 1)));
  31. BOOST_CHECK(subset(pi_f, widen(I_f((float) PI), (std::numeric_limits<float> ::min)())));
  32. BOOST_CHECK(subset(pi_d, widen(I_d((double)PI), (std::numeric_limits<double>::min)())));
  33. // We can't test the following equalities for interval<int>.
  34. I_f pi_f_half = pi_half<I_f>();
  35. I_f pi_f_twice = pi_twice<I_f>();
  36. I_d pi_d_half = pi_half<I_d>();
  37. I_d pi_d_twice = pi_twice<I_d>();
  38. I_ld pi_ld_half = pi_half<I_ld>();
  39. I_ld pi_ld_twice = pi_twice<I_ld>();
  40. BOOST_CHECK(equal(2.0f * pi_f_half, pi_f));
  41. BOOST_CHECK(equal(2.0 * pi_d_half, pi_d));
  42. BOOST_CHECK(equal(2.0l * pi_ld_half, pi_ld));
  43. BOOST_CHECK(equal(2.0f * pi_f, pi_f_twice));
  44. BOOST_CHECK(equal(2.0 * pi_d, pi_d_twice));
  45. BOOST_CHECK(equal(2.0l * pi_ld, pi_ld_twice));
  46. return 0;
  47. }