big_seventh.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. // Copyright Paul A. Bristow 2012.
  6. // Copyright Christopher Kormanyos 2012.
  7. // This file is written to be included from a Quickbook .qbk document.
  8. // It can be compiled by the C++ compiler, and run. Any output can
  9. // also be added here as comment or included or pasted in elsewhere.
  10. // Caution: this file contains Quickbook markup as well as code
  11. // and comments: don't change any of the special comment markups!
  12. #ifdef _MSC_VER
  13. # pragma warning (disable : 4512) // assignment operator could not be generated.
  14. # pragma warning (disable : 4996)
  15. #endif
  16. //[big_seventh_example_1
  17. /*`[h5 Using Boost.Multiprecision `cpp_float` for numerical calculations with high precision.]
  18. The Boost.Multiprecision library can be used for computations requiring precision
  19. exceeding that of standard built-in types such as float, double
  20. and long double. For extended-precision calculations, Boost.Multiprecision
  21. supplies a template data type called cpp_dec_float. The number of decimal
  22. digits of precision is fixed at compile-time via template parameter.
  23. To use these floating-point types and constants, we need some includes:
  24. */
  25. #include <boost/math/constants/constants.hpp>
  26. #include <boost/multiprecision/cpp_dec_float.hpp>
  27. // using boost::multiprecision::cpp_dec_float
  28. #include <iostream>
  29. #include <limits>
  30. /*` So now we can demonstrate with some trivial calculations:
  31. */
  32. //] //[big_seventh_example_1]
  33. int main()
  34. {
  35. //[big_seventh_example_2
  36. /*`Using `typedef cpp_dec_float_50` hides the complexity of multiprecision,
  37. allows us to define variables with 50 decimal digit precision just like built-in `double`.
  38. */
  39. using boost::multiprecision::cpp_dec_float_50;
  40. cpp_dec_float_50 seventh = cpp_dec_float_50(1) / 7; // 1 / 7
  41. /*`By default, output would only show the standard 6 decimal digits,
  42. so set precision to show all 50 significant digits, including any trailing zeros.
  43. */
  44. std::cout.precision(std::numeric_limits<cpp_dec_float_50>::digits10);
  45. std::cout << std::showpoint << std::endl; // Append any trailing zeros.
  46. std::cout << seventh << std::endl;
  47. /*`which outputs:
  48. 0.14285714285714285714285714285714285714285714285714
  49. We can also use Boost.Math __constants like [pi],
  50. guaranteed to be initialized with the very last bit of precision for the floating-point type.
  51. */
  52. std::cout << "pi = " << boost::math::constants::pi<cpp_dec_float_50>() << std::endl;
  53. cpp_dec_float_50 circumference = boost::math::constants::pi<cpp_dec_float_50>() * 2 * seventh;
  54. std::cout << "c = "<< circumference << std::endl;
  55. /*`which outputs
  56. pi = 3.1415926535897932384626433832795028841971693993751
  57. c = 0.89759790102565521098932668093700082405633411410717
  58. */
  59. //] [/big_seventh_example_2]
  60. return 0;
  61. } // int main()
  62. /*
  63. //[big_seventh_example_output
  64. 0.14285714285714285714285714285714285714285714285714
  65. pi = 3.1415926535897932384626433832795028841971693993751
  66. c = 0.89759790102565521098932668093700082405633411410717
  67. //] //[big_seventh_example_output]
  68. */