Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / multiprecision / test / test_cpp_dec_float_round.cpp
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 http://www.boost.org/LICENSE_1_
5 //
6
7 // Test case for ticket:
8 // #8065: Multiprecision rounding issue
9
10 #ifdef _MSC_VER
11 #  define _SCL_SECURE_NO_WARNINGS
12 #endif
13
14 #include <boost/detail/lightweight_test.hpp>
15 #include "test.hpp"
16 #include <boost/multiprecision/cpp_dec_float.hpp>
17 #include <boost/math/special_functions/round.hpp>
18
19 template<int N>
20 static bool round_test_imp()
21 {
22   typedef boost::multiprecision::cpp_dec_float<N> mp_backend_type;
23   typedef boost::multiprecision::number<mp_backend_type, boost::multiprecision::et_off> mp_type;
24
25   const mp_type original_digits(1.0F);
26
27   const mp_type scale = pow(mp_type(10), N);
28
29   mp_type these_digits = original_digits * scale;
30
31   these_digits  = boost::math::round(these_digits);
32   these_digits /= scale;
33
34   const std::string result = these_digits.str();
35
36   return (result == original_digits.str());
37 }
38
39 template<int N>
40 struct round_test
41 {
42   static bool test()
43   {
44     return (round_test_imp<N>() && round_test<N - 1>::test());
45   }
46 };
47
48 template<>
49 struct round_test<0>
50 {
51   static bool test()
52   {
53     return round_test_imp<0>();
54   }
55 };
56
57 int main()
58 {
59    //
60    // Test cpp_dec_float rounding with boost::math::round() at various precisions:
61    //
62    const bool round_test_result = round_test<40>::test();
63
64    BOOST_CHECK_EQUAL(round_test_result, true);
65
66    return boost::report_errors();
67 }