1 // Use, modification and distribution are subject to the
2 // Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE_1_0.txt
4 // or copy at http://www.boost.org/LICENSE_1_0.txt)
6 // Copyright Paul A. Bristow 2012.
7 // Copyright Christopher Kormanyos 2012.
9 // This file is written to be included from a Quickbook .qbk document.
10 // It can be compiled by the C++ compiler, and run. Any output can
11 // also be added here as comment or included or pasted in elsewhere.
12 // Caution: this file contains Quickbook markup as well as code
13 // and comments: don't change any of the special comment markups!
16 # pragma warning (disable : 4512) // assignment operator could not be generated.
17 # pragma warning (disable : 4996)
20 //[big_seventh_example_1
22 /*`[h5 Using Boost.Multiprecision `cpp_float` for numerical calculations with high precision.]
24 The Boost.Multiprecision library can be used for computations requiring precision
25 exceeding that of standard built-in types such as float, double
26 and long double. For extended-precision calculations, Boost.Multiprecision
27 supplies a template data type called cpp_dec_float. The number of decimal
28 digits of precision is fixed at compile-time via template parameter.
30 To use these floating-point types and constants, we need some includes:
34 #include <boost/math/constants/constants.hpp>
36 #include <boost/multiprecision/cpp_dec_float.hpp>
37 // using boost::multiprecision::cpp_dec_float
42 /*` So now we can demonstrate with some trivial calculations:
45 //] //[big_seventh_example_1]
50 //[big_seventh_example_2
51 /*`Using `typedef cpp_dec_float_50` hides the complexity of multiprecision,
52 allows us to define variables with 50 decimal digit precision just like built-in `double`.
54 using boost::multiprecision::cpp_dec_float_50;
56 cpp_dec_float_50 seventh = cpp_dec_float_50(1) / 7; // 1 / 7
58 /*`By default, output would only show the standard 6 decimal digits,
59 so set precision to show all 50 significant digits, including any trailing zeros.
61 std::cout.precision(std::numeric_limits<cpp_dec_float_50>::digits10);
62 std::cout << std::showpoint << std::endl; // Append any trailing zeros.
63 std::cout << seventh << std::endl;
66 0.14285714285714285714285714285714285714285714285714
68 We can also use Boost.Math __constants like [pi],
69 guaranteed to be initialized with the very last bit of precision for the floating-point type.
72 std::cout << "pi = " << boost::math::constants::pi<cpp_dec_float_50>() << std::endl;
73 cpp_dec_float_50 circumference = boost::math::constants::pi<cpp_dec_float_50>() * 2 * seventh;
74 std::cout << "c = "<< circumference << std::endl;
78 pi = 3.1415926535897932384626433832795028841971693993751
80 c = 0.89759790102565521098932668093700082405633411410717
82 //] [/big_seventh_example_2]
89 //[big_seventh_example_output
91 0.14285714285714285714285714285714285714285714285714
92 pi = 3.1415926535897932384626433832795028841971693993751
93 c = 0.89759790102565521098932668093700082405633411410717
95 //] //[big_seventh_example_output]