test_cpp_dec_float_round.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. ///////////////////////////////////////////////////////////////
  2. // Copyright 2013 Christopher Kormanyos. Distributed under the Boost
  3. // Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
  5. //
  6. // Test case for ticket:
  7. // #8065: Multiprecision rounding issue
  8. #ifdef _MSC_VER
  9. #define _SCL_SECURE_NO_WARNINGS
  10. #endif
  11. #include <boost/detail/lightweight_test.hpp>
  12. #include "test.hpp"
  13. #include <boost/multiprecision/cpp_dec_float.hpp>
  14. #include <boost/math/special_functions/round.hpp>
  15. template <int N>
  16. static bool round_test_imp()
  17. {
  18. typedef boost::multiprecision::cpp_dec_float<N> mp_backend_type;
  19. typedef boost::multiprecision::number<mp_backend_type, boost::multiprecision::et_off> mp_type;
  20. const mp_type original_digits(1.0F);
  21. const mp_type scale = pow(mp_type(10), N);
  22. mp_type these_digits = original_digits * scale;
  23. these_digits = boost::math::round(these_digits);
  24. these_digits /= scale;
  25. const std::string result = these_digits.str();
  26. return (result == original_digits.str());
  27. }
  28. template <int N>
  29. struct round_test
  30. {
  31. static bool test()
  32. {
  33. return (round_test_imp<N>() && round_test<N - 1>::test());
  34. }
  35. };
  36. template <>
  37. struct round_test<0>
  38. {
  39. static bool test()
  40. {
  41. return round_test_imp<0>();
  42. }
  43. };
  44. int main()
  45. {
  46. //
  47. // Test cpp_dec_float rounding with boost::math::round() at various precisions:
  48. //
  49. const bool round_test_result = round_test<40>::test();
  50. BOOST_CHECK_EQUAL(round_test_result, true);
  51. return boost::report_errors();
  52. }