Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / math / example / test_cpp_float_close_fraction.cpp
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)
4
5 // Copyright Paul A. Bristow 2013
6 // Copyright Christopher Kormanyos 2013.
7 // Copyright John Maddock 2013.
8
9 #ifdef _MSC_VER
10 #  pragma warning (disable : 4512)
11 #  pragma warning (disable : 4996)
12 #endif
13
14 #define BOOST_TEST_MAIN
15 #define BOOST_LIB_DIAGNOSTIC "on"// Show library file details.
16
17 #include <boost/test/unit_test.hpp>
18 #include <boost/test/floating_point_comparison.hpp> // Extra test tool for FP comparison.
19
20 #include <iostream>
21 #include <limits>
22
23 //[expression_template_1
24
25 #include <boost/multiprecision/cpp_dec_float.hpp>
26
27 /*`To define a 50 decimal digit type using `cpp_dec_float`, 
28 you must pass two template parameters to `boost::multiprecision::number`.
29
30 It may be more legible to use a two-staged type definition such as this:
31
32 ``
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;
35 ``
36
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.
40
41   typedef boost::multiprecision::number<boost::multiprecision::cpp_dec_float<50>, boost::multiprecision::et_off> 
42   cpp_dec_float_50_noet;
43
44 You can reduce typing with a `using` directive `using namespace boost::multiprecision;`
45 if desired, as shown below.
46 */
47
48 using namespace boost::multiprecision;
49     
50 typedef number<cpp_dec_float<50>, et_off> cpp_dec_float_50_noet;
51
52 //`Now `cpp_dec_float_50_noet` can be used as a direct replacement for built-in types like `double` etc.
53
54 BOOST_AUTO_TEST_CASE(cpp_float_test_check_close)
55 {
56
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.
59
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.
63
64   cpp_dec_float_50_noet eps = std::numeric_limits<cpp_dec_float_50_noet>::epsilon();
65
66   std::cout <<"a = " << a << ",\nb = " << b << ",\neps = " << eps << std::endl;
67
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).
70
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:
73 */
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>'
76   
77 //] [/expression_template_1]
78
79 } // BOOST_AUTO_TEST_CASE(cpp_float_test_check_close)
80
81 /*
82
83 Output:
84
85   Description: Autorun "J:\Cpp\big_number\Debug\test_cpp_float_close_fraction.exe"
86   Running 1 test case...
87   
88   a = 1.0000000000000000000000000000000000000000000000000,
89   b = 1.0000000000000000000000000000000000000000000000001,
90   eps = 1.0000000000000000000000000000000000000000000000000e-49
91   
92   *** No errors detected
93
94
95 */
96