-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtommath_factorial.cpp
More file actions
53 lines (43 loc) · 1.26 KB
/
tommath_factorial.cpp
File metadata and controls
53 lines (43 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
* g++ -Wall -O2 -std=c++11 -IC:\Tools\boost_1_69_0
* -IC:\Tools\libtommath-1.1.0
* -o tommath_factorial.exe tommath_factorial.cpp
* -ltommath
*/
#include <boost/multiprecision/tommath.hpp>
#include <iostream>
using namespace std;
int main()
{
boost::multiprecision::tom_int v = 1;
// Do some arithmetic:
for(unsigned i = 1; i <= 1000; ++i)
v *= i;
cout << v << endl << endl; // prints 1000!
cout << hex << v << endl << endl; // prints 1000! in hex format
cout << "Press Enter: ";
getchar();
// Do more arithmetic
for(unsigned i = 1001; i <= 2000; i++)
v *= i;
cout << endl << dec << v << endl << endl; // prints 1000!
cout << hex << v << endl << endl; // prints 1000! in hex format
try {
cout << hex << -v << endl; // Ooops! can't print a negative value in hex format!
}
catch(const runtime_error& e)
{
cout << e.what() << endl;
}
try {
// v is not a 2's complement type, bitwise operations are only supported
// on positive values:
v = -v & 2;
}
catch(const runtime_error& e)
{
cout << e.what() << endl;
cout << "Okay, then." << endl;
}
return 0;
}