1 // Use, modification and distribution are subject to the
2 // Boost Software License, Version 1.0. (See accompanying file
3 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 // Copyright Paul A. Bristow 2013
6 // Copyright Christopher Kormanyos 2013.
7 // Copyright John Maddock 2013.
10 # pragma warning (disable : 4512)
11 # pragma warning (disable : 4996)
14 #define BOOST_TEST_MAIN
15 #define BOOST_LIB_DIAGNOSTIC "on"// Show library file details.
17 #include <boost/test/unit_test.hpp>
18 #include <boost/test/floating_point_comparison.hpp> // Extra test tool for FP comparison.
23 //[expression_template_1
25 #include <boost/multiprecision/cpp_dec_float.hpp>
27 /*`To define a 50 decimal digit type using `cpp_dec_float`,
28 you must pass two template parameters to `boost::multiprecision::number`.
30 It may be more legible to use a two-staged type definition such as this:
33 typedef boost::multiprecision::cpp_dec_float<50> mp_backend;
34 typedef boost::multiprecision::number<mp_backend, boost::multiprecision::et_off> cpp_dec_float_50_noet;
37 Here, we first define `mp_backend` as `cpp_dec_float` with 50 digits.
38 The second step passes this backend to `boost::multiprecision::number`
39 with `boost::multiprecision::et_off`, an enumerated type.
41 typedef boost::multiprecision::number<boost::multiprecision::cpp_dec_float<50>, boost::multiprecision::et_off>
42 cpp_dec_float_50_noet;
44 You can reduce typing with a `using` directive `using namespace boost::multiprecision;`
45 if desired, as shown below.
48 using namespace boost::multiprecision
;
51 /*`Now `cpp_dec_float_50_noet` or `cpp_dec_float_50_et`
52 can be used as a direct replacement for built-in types like `double` etc.
55 BOOST_AUTO_TEST_CASE(cpp_float_test_check_close_noet
)
56 { // No expression templates/
57 typedef number
<cpp_dec_float
<50>, et_off
> cpp_dec_float_50_noet
;
59 std::cout
.precision(std::numeric_limits
<cpp_dec_float_50_noet
>::digits10
); // All significant digits.
60 std::cout
<< std::showpoint
<< std::endl
; // Show trailing zeros.
62 cpp_dec_float_50_noet
a ("1.0");
63 cpp_dec_float_50_noet
b ("1.0");
64 b
+= std::numeric_limits
<cpp_dec_float_50_noet
>::epsilon(); // Increment least significant decimal digit.
66 cpp_dec_float_50_noet eps
= std::numeric_limits
<cpp_dec_float_50_noet
>::epsilon();
68 std::cout
<<"a = " << a
<< ",\nb = " << b
<< ",\neps = " << eps
<< std::endl
;
70 BOOST_CHECK_CLOSE(a
, b
, eps
* 100); // Expected to pass (because tolerance is as percent).
71 BOOST_CHECK_CLOSE_FRACTION(a
, b
, eps
); // Expected to pass (because tolerance is as fraction).
75 } // BOOST_AUTO_TEST_CASE(cpp_float_test_check_close)
77 BOOST_AUTO_TEST_CASE(cpp_float_test_check_close_et
)
78 { // Using expression templates.
79 typedef number
<cpp_dec_float
<50>, et_on
> cpp_dec_float_50_et
;
81 std::cout
.precision(std::numeric_limits
<cpp_dec_float_50_et
>::digits10
); // All significant digits.
82 std::cout
<< std::showpoint
<< std::endl
; // Show trailing zeros.
84 cpp_dec_float_50_et
a("1.0");
85 cpp_dec_float_50_et
b("1.0");
86 b
+= std::numeric_limits
<cpp_dec_float_50_et
>::epsilon(); // Increment least significant decimal digit.
88 cpp_dec_float_50_et eps
= std::numeric_limits
<cpp_dec_float_50_et
>::epsilon();
90 std::cout
<< "a = " << a
<< ",\nb = " << b
<< ",\neps = " << eps
<< std::endl
;
92 BOOST_CHECK_CLOSE(a
, b
, eps
* 100); // Expected to pass (because tolerance is as percent).
93 BOOST_CHECK_CLOSE_FRACTION(a
, b
, eps
); // Expected to pass (because tolerance is as fraction).
95 /*`Using `cpp_dec_float_50` with the default expression template use switched on,
96 the compiler error message for `BOOST_CHECK_CLOSE_FRACTION(a, b, eps); would be:
98 // failure floating_point_comparison.hpp(59): error C2440: 'static_cast' :
99 // cannot convert from 'int' to 'boost::multiprecision::detail::expression<tag,Arg1,Arg2,Arg3,Arg4>'
100 //] [/expression_template_1]
102 } // BOOST_AUTO_TEST_CASE(cpp_float_test_check_close)
108 Description: Autorun "J:\Cpp\big_number\Debug\test_cpp_float_close_fraction.exe"
109 Running 1 test case...
111 a = 1.0000000000000000000000000000000000000000000000000,
112 b = 1.0000000000000000000000000000000000000000000000001,
113 eps = 1.0000000000000000000000000000000000000000000000000e-49
115 *** No errors detected