• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 ///////////////////////////////////////////////////////////////
2 //  Copyright 2011 John Maddock. Distributed under the Boost
3 //  Software License, Version 1.0. (See accompanying file
4 //  LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
5 
6 #include <boost/multiprecision/gmp.hpp>
7 #include <boost/math/special_functions/gamma.hpp>
8 #include <iostream>
9 
t1()10 void t1()
11 {
12    //[mpz_eg
13 //=#include <boost/multiprecision/gmp.hpp>
14 //=#include <iostream>
15 //=
16 //=int main()
17 //={
18    using namespace boost::multiprecision;
19 
20    mpz_int v = 1;
21 
22    // Do some arithmetic:
23    for(unsigned i = 1; i <= 1000; ++i)
24       v *= i;
25 
26    std::cout << v << std::endl; // prints 1000!
27 
28    // Access the underlying representation:
29    mpz_t z;
30    mpz_init(z);
31    mpz_set(z, v.backend().data());
32    mpz_clear(z);
33 //=   return 0;
34 //=}
35 //]
36 }
37 
t2()38 void t2()
39 {
40 //[mpf_eg
41 //=#include <boost/multiprecision/gmp.hpp>
42 //=#include <boost/math/special_functions/gamma.hpp>
43 //=#include <iostream>
44 //=
45 //=int main()
46 //={
47    using namespace boost::multiprecision;
48 
49    // Operations at variable precision and limited standard library support:
50    mpf_float a = 2;
51    mpf_float::default_precision(1000);
52    std::cout << mpf_float::default_precision() << std::endl;
53    std::cout << sqrt(a) << std::endl; // print root-2
54 
55    // Operations at fixed precision and full standard library support:
56    mpf_float_100 b = 2;
57    std::cout << std::numeric_limits<mpf_float_100>::digits << std::endl;
58    // We can use any C++ std lib function:
59    std::cout << log(b) << std::endl; // print log(2)
60    // We can also use any function from Boost.Math:
61    std::cout << boost::math::tgamma(b) << std::endl;
62    // These even work when the argument is an expression template:
63    std::cout << boost::math::tgamma(b * b) << std::endl;
64 
65    // Access the underlying representation:
66    mpf_t f;
67    mpf_init(f);
68    mpf_set(f, a.backend().data());
69    mpf_clear(f);
70 //=   return 0;
71 //=}
72 //]
73 }
74 
t3()75 void t3()
76 {
77 //[mpq_eg
78 //=#include <boost/multiprecision/gmp.hpp>
79 //=#include <boost/multiprecision/gmp.hpp>
80 //=#include <iostream>
81 //=
82 //=int main()
83 //={
84    using namespace boost::multiprecision;
85 
86    mpq_rational v = 1;
87 
88    // Do some arithmetic:
89    for(unsigned i = 1; i <= 1000; ++i)
90       v *= i;
91    v /= 10;
92 
93    std::cout << v << std::endl; // prints 1000! / 10
94    std::cout << numerator(v) << std::endl;
95    std::cout << denominator(v) << std::endl;
96 
97    mpq_rational w(2, 3);  // component wise constructor
98    std::cout << w << std::endl; // prints 2/3
99 
100    // Access the underlying data:
101    mpq_t q;
102    mpq_init(q);
103    mpq_set(q, v.backend().data());
104    mpq_clear(q);
105 //=   return 0;
106 //=}
107 //]
108 }
109 
main()110 int main()
111 {
112    t1();
113    t2();
114    t3();
115    return 0;
116 }
117 
118