Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / math / example / big_seventh.cpp
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)
5
6 // Copyright Paul A. Bristow 2012.
7 // Copyright Christopher Kormanyos 2012.
8
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!
14
15 #ifdef _MSC_VER
16 #  pragma warning (disable : 4512) // assignment operator could not be generated.
17 #  pragma warning (disable : 4996)
18 #endif
19
20 //[big_seventh_example_1
21
22 /*`[h5 Using Boost.Multiprecision `cpp_float` for numerical calculations with high precision.]
23
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.
29
30 To use these floating-point types and constants, we need some includes:
31
32 */
33
34 #include <boost/math/constants/constants.hpp>
35
36 #include <boost/multiprecision/cpp_dec_float.hpp>
37 // using boost::multiprecision::cpp_dec_float
38
39 #include <iostream>
40 #include <limits>
41
42 /*` So now we can demonstrate with some trivial calculations:
43 */
44
45 //] //[big_seventh_example_1]
46
47 int main()
48 {
49
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`.
53 */
54   using boost::multiprecision::cpp_dec_float_50;
55
56   cpp_dec_float_50 seventh = cpp_dec_float_50(1) / 7;  // 1 / 7
57
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.
60 */
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;
64 /*`which outputs:
65
66   0.14285714285714285714285714285714285714285714285714
67
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.
70 */
71
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;
75
76 /*`which outputs
77
78   pi = 3.1415926535897932384626433832795028841971693993751
79
80   c =  0.89759790102565521098932668093700082405633411410717
81 */
82 //]  [/big_seventh_example_2]
83
84     return 0;
85 } // int main()
86
87
88 /*
89 //[big_seventh_example_output
90
91 0.14285714285714285714285714285714285714285714285714
92 pi = 3.1415926535897932384626433832795028841971693993751
93 c =  0.89759790102565521098932668093700082405633411410717
94
95 //] //[big_seventh_example_output]
96
97 */
98