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;
50 typedef number<cpp_dec_float<50>, et_off> cpp_dec_float_50_noet;
52 //`Now `cpp_dec_float_50_noet` can be used as a direct replacement for built-in types like `double` etc.
54 BOOST_AUTO_TEST_CASE(cpp_float_test_check_close)
57 std::cout.precision(std::numeric_limits<cpp_dec_float_50_noet>::digits10); // All significant digits.
58 std::cout << std::showpoint << std::endl; // Show trailing zeros.
60 cpp_dec_float_50_noet a ("1.");
61 cpp_dec_float_50_noet b ("1.");
62 b += std::numeric_limits<cpp_dec_float_50_noet>::epsilon(); // Increment least significant decimal digit.
64 cpp_dec_float_50_noet eps = std::numeric_limits<cpp_dec_float_50_noet>::epsilon();
66 std::cout <<"a = " << a << ",\nb = " << b << ",\neps = " << eps << std::endl;
68 BOOST_CHECK_CLOSE(a, b, eps * 100); // Expected to pass (because tolerance is as percent).
69 BOOST_CHECK_CLOSE_FRACTION(a, b, eps); // Expected to pass (because tolerance is as fraction).
71 /*`Using `cpp_dec_float_50` with the default expression template use switched on,
72 the compiler error message for `BOOST_CHECK_CLOSE_FRACTION(a, b, eps); would be:
74 // failure floating_point_comparison.hpp(59): error C2440: 'static_cast' :
75 // cannot convert from 'int' to 'boost::multiprecision::detail::expression<tag,Arg1,Arg2,Arg3,Arg4>'
77 //] [/expression_template_1]
79 } // BOOST_AUTO_TEST_CASE(cpp_float_test_check_close)
85 Description: Autorun "J:\Cpp\big_number\Debug\test_cpp_float_close_fraction.exe"
86 Running 1 test case...
88 a = 1.0000000000000000000000000000000000000000000000000,
89 b = 1.0000000000000000000000000000000000000000000000001,
90 eps = 1.0000000000000000000000000000000000000000000000000e-49
92 *** No errors detected