bigint-2010.04.30
[bigint/bigint.git] / sample.cc
... / ...
CommitLineData
1// Sample program demonstrating the use of the Big Integer Library.
2
3// Standard libraries
4#include <string>
5#include <iostream>
6
7// `BigIntegerLibrary.hh' includes all of the library headers.
8#include "BigIntegerLibrary.hh"
9
10int main() {
11 /* The library throws `const char *' error messages when things go
12 * wrong. It's a good idea to catch them using a `try' block like this
13 * one. Your C++ compiler might need a command-line option to compile
14 * code that uses exceptions. */
15 try {
16 BigInteger a; // a is 0
17 int b = 535;
18
19 /* Any primitive integer can be converted implicitly to a
20 * BigInteger. */
21 a = b;
22
23 /* The reverse conversion requires a method call (implicit
24 * conversions were previously supported but caused trouble).
25 * If a were too big for an int, the library would throw an
26 * exception. */
27 b = a.toInt();
28
29 BigInteger c(a); // Copy a BigInteger.
30
31 // The int literal is converted to a BigInteger.
32 BigInteger d(-314159265);
33
34 /* This won't compile (at least on 32-bit machines) because the
35 * number is too big to be a primitive integer literal, and
36 * there's no such thing as a BigInteger literal. */
37 //BigInteger e(3141592653589793238462643383279);
38
39 // Instead you can convert the number from a string.
40 std::string s("3141592653589793238462643383279");
41 BigInteger f = stringToBigInteger(s);
42
43 // You can convert the other way too.
44 std::string s2 = bigIntegerToString(f);
45
46 // f is implicitly stringified and sent to std::cout.
47 std::cout << f << std::endl;
48
49 /* Let's do some math! The library overloads most of the
50 * mathematical operators (including assignment operators) to
51 * work on BigIntegers. There are also ``copy-less''
52 * operations; see `BigUnsigned.hh' for details. */
53
54 // Arithmetic operators
55 BigInteger g(314159), h(265);
56 std::cout << (g + h) << '\n'
57 << (g - h) << '\n'
58 << (g * h) << '\n'
59 << (g / h) << '\n'
60 << (g % h) << std::endl;
61
62 // Bitwise operators
63 BigUnsigned i(0xFF0000FF), j(0x0000FFFF);
64 // The library's << operator recognizes base flags.
65 std::cout.flags(std::ios::hex | std::ios::showbase);
66 std::cout << (i & j) << '\n'
67 << (i | j) << '\n'
68 << (i ^ j) << '\n'
69 // Shift distances are ordinary unsigned ints.
70 << (j << 21) << '\n'
71 << (j >> 10) << '\n';
72 std::cout.flags(std::ios::dec);
73
74 // Let's do some heavy lifting and calculate powers of 314.
75 int maxPower = 10;
76 BigUnsigned x(1), big314(314);
77 for (int power = 0; power <= maxPower; power++) {
78 std::cout << "314^" << power << " = " << x << std::endl;
79 x *= big314; // A BigInteger assignment operator
80 }
81
82 // Some big-integer algorithms (albeit on small integers).
83 std::cout << gcd(BigUnsigned(60), 72) << '\n'
84 << modinv(BigUnsigned(7), 11) << '\n'
85 << modexp(BigUnsigned(314), 159, 2653) << std::endl;
86
87 // Add your own code here to experiment with the library.
88 } catch(char const* err) {
89 std::cout << "The library threw an exception:\n"
90 << err << std::endl;
91 }
92
93 return 0;
94}
95
96/*
97The original sample program produces this output:
98
993141592653589793238462643383279
100314424
101313894
10283252135
1031185
104134
1050xFF
1060xFF00FFFF
1070xFF00FF00
1080x1FFFE00000
1090x3F
110314^0 = 1
111314^1 = 314
112314^2 = 98596
113314^3 = 30959144
114314^4 = 9721171216
115314^5 = 3052447761824
116314^6 = 958468597212736
117314^7 = 300959139524799104
118314^8 = 94501169810786918656
119314^9 = 29673367320587092457984
120314^10 = 9317437338664347031806976
12112
1228
1231931
124
125*/