Old snapshot `BigIntegerLibrary-2004.12.24.2'; see the ChangeLog file.
[bigint/bigint.git] / sample.cc
CommitLineData
05780f4b
MM
1/*
2* Matt McCutchen's Big Integer Library
3* http://mysite.verizon.net/mccutchen/bigint/
4*/
5
6/*
7* This sample file demonstrates the most important features of the Big Integer Library.
8*
9* To get started quickly with the library, imitate the code in `main' below.
10*
11* If you want more detail or more speed or can't find a feature here,
12* look in the appropriate source file. This file shows only the more ``user-friendly'' features;
13* the other features are messier but worth learning eventually.
14*
15* GO FORTH and play with many-digit numbers! (c.f. The TeXbook.)
16*/
17
18#include "BigUnsigned.hh"
19#include "BigInteger.hh"
20#include "BigIntegerUtils.hh"
21
22#include <string>
23#include <iostream>
24
25int main() {
26 try {
27 BigInteger a; // a is 0
28 int b = 535;
29
30 a = b; // From int to BigUnsigned...
31 b = a; // ...and back, no casts required!
32 /*
33 * If a were too big for an int you'd get a runtime exception. The Big Integer Library
34 * throws C-strings (that is, `const char *'s) when something goes wrong. It's a good
35 * idea to catch them. Some C++ compilers need a special command-line option to compile
36 * code that uses throw/catch.
37 */
38
39 BigInteger c(a); // Copy them.
40
41 BigInteger d(-314159265); // c is -314159265. The `int' literal is converted to a BigInteger.
42
43 // Ahem: that's too big to be an `int' literal (or even a `long' literal)!
44 // Disillusion yourself now -- this won't compile.
45 //BigInteger e(3141592653589793238462643383279);
46
47 std::string s("3141592653589793238462643383279");
48 BigInteger f = easyStringToBI(s);
49 // Ah. The string is converted to a BigInteger, and strings can be as long as you want.
50
51 std::string s2 = easyBItoString(f); // You can convert the other way too.
52
53 std::cout << f << std::endl; // f is stringified and send to std::cout.
54
55 /*
56 * Let's do some math!
57 *
58 * The Big Integer Library provides three kinds of operators:
59 *
60 * (1) Overloaded ``value'' operators: +, -, *, /, %, unary -.
61 * Big-integer code using these operators looks identical to
62 * code using the primitive integer types. The operator takes
63 * one or two BigInteger inputs and returns a BigInteger result,
64 * which can then be assigned to a BigInteger variable or used
65 * in an expression.
66 *
67 * (2) Overloaded assignment operators: +=, -=, *=, /=, %=,
68 * ++, --, flipSign.
69 * Again, these are used on BigIntegers just like on ints.
70 * They take one writable BigInteger that both provides an
71 * operand and receives a result. The first five also take
72 * a second read-only operand.
73 *
74 * (3) ``Put-here'' operations: `add', `subtract', etc.
75 * Use these if and only if you are concerned about performance.
76 * They require fewer BigInteger copy-constructions and assignments
77 * than do operators in (1) or (2). Most take two read-only operands
78 * and save the result in the invoked object `*this', whose previous
79 * value is irrelevant. `divideWithRemainder' is an exception.
80 * <<< NOTE >>>: Put-here operations do not return a value: they don't need to!!
81 */
82
83 BigInteger g(314159), h(265);
84 // All five ``value'' operators
85 std::cout << (g + h) << '\n' << (g - h) << '\n' << (g * h)
86 << '\n' << (g / h) << '\n' << (g % h) << std::endl;
87
88 BigInteger i(5), j(10), k;
89 // These two lines do the same thing: k is set to a BigInteger containing 15.
90 k = i + j;
91 k.add(i, j);
92
93 // Let's do some heavy lifting.
94 std::cout << "Powers of 3" << std::endl;
95 std::cout << "How many do you want?" << std::endl;
96 int maxPower;
97 std::cin >> maxPower;
98
99 BigUnsigned x(1), three(3);
100 for (int power = 0; power <= maxPower; power++) {
101 std::cout << "3^" << power << " = " << x << std::endl;
102 x *= three; // A BigInteger assignment operator
103 }
104
105 std::cout << "There you go. Goodbye." << std::endl;
106
107 } catch(char const* err){
108 std::cout << "Sorry, the library threw an exception:\n"
109 << err << std::endl;
110 }
111 return 0;
112}
113
114/*
115* Here is the output of a sample run of this sample program:
116
1173141592653589793238462643383279
118314424
119313894
12083252135
1211185
122134
123Powers of 3
124How many do you want?
1252
1263^0 = 1
1273^1 = 3
1283^2 = 9
129There you go. Goodbye.
130
131*/