• 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/tommath.hpp>
7 #include <iostream>
8 
t1()9 void t1()
10 {
11 //[tommath_eg
12 //=#include <boost/multiprecision/tommath.hpp>
13 //=#include <iostream>
14 //=
15 //=int main()
16 //={
17    boost::multiprecision::tom_int v = 1;
18 
19    // Do some arithmetic:
20    for(unsigned i = 1; i <= 1000; ++i)
21       v *= i;
22 
23    std::cout << v << std::endl; // prints 1000!
24    std::cout << std::hex << v << std::endl; // prints 1000! in hex format
25 
26    try{
27       std::cout << std::hex << -v << std::endl; // Ooops! can't print a negative value in hex format!
28    }
29    catch(const std::runtime_error& e)
30    {
31       std::cout << e.what() << std::endl;
32    }
33 
34    try{
35       // v is not a 2's complement type, bitwise operations are only supported
36       // on positive values:
37       v = -v & 2;
38    }
39    catch(const std::runtime_error& e)
40    {
41       std::cout << e.what() << std::endl;
42    }
43 
44 //=   return 0;
45 //=}
46 //]
47 }
48 
t3()49 void t3()
50 {
51 //[mp_rat_eg
52 //=#include <boost/multiprecision/tommath.hpp>
53 //=#include <iostream>
54 //=
55 //=int main()
56 //={
57    using namespace boost::multiprecision;
58 
59    tom_rational v = 1;
60 
61    // Do some arithmetic:
62    for(unsigned i = 1; i <= 1000; ++i)
63       v *= i;
64    v /= 10;
65 
66    std::cout << v << std::endl; // prints 1000! / 10
67    std::cout << numerator(v) << std::endl;
68    std::cout << denominator(v) << std::endl;
69 
70    tom_rational w(2, 3); // Component wise constructor
71    std::cout << w << std::endl; // prints 2/3
72 
73 //=   return 0;
74 //=}
75 //]
76 }
77 
main()78 int main()
79 {
80    t1();
81    t3();
82    return 0;
83 }
84 
85