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