2 * Matt McCutchen's Big Integer Library
3 * http://hashproduct.metaesthetics.net/bigint/
7 * Milan Tomic had trouble compiling this file on Microsoft
8 * Visual C++ 6 because, in the libraries that come with
9 * Visual C++ 6, the `std::string::push_back' method apparently
10 * does not exist. To get around the problem, I rewrote
11 * `BigUnsignedInABase::operator std::string' (at the bottom
12 * of this file) so it doesn't use `push_back'.
15 #include "BigUnsignedInABase.hh"
18 unsigned int bitLen(unsigned int x) {
26 unsigned int ceilingDiv(unsigned int a, unsigned int b) {
27 return (a + b - 1) / b;
31 BigUnsignedInABase::BigUnsignedInABase(const BigUnsigned &x, Base base) {
35 throw "BigUnsignedInABase(BigUnsigned, Base): The base must be at least 2";
37 // This pattern is seldom seen in C++, but the analogous ``this.'' is common in Java.
40 // Get an upper bound on how much space we need
41 int maxBitLenOfX = x.getLength() * BigUnsigned::N;
42 int minBitsPerDigit = bitLen(base) - 1;
43 int maxDigitLenOfX = ceilingDiv(maxBitLenOfX, minBitsPerDigit);
44 len = maxDigitLenOfX; // Another change to comply with `staying in bounds'; see `BigUnsigned::divideWithRemainder'.
45 allocate(len); // Get the space
47 BigUnsigned x2(x), buBase(base);
50 while (!x2.isZero()) {
51 // Get last digit. This is like `lastDigit = x2 % buBase, x2 /= buBase'.
52 BigUnsigned lastDigit(x2);
53 lastDigit.divideWithRemainder(buBase, x2);
55 blk[digitNum] = Digit(lastDigit); // invokes `BigUnsigned ==> unsigned short' converter
56 // Move on. We can't run out of room: we figured it out above.
60 // Save the actual length.
64 BigUnsignedInABase::operator BigUnsigned() const {
65 BigUnsigned ans(0), buBase(base), temp;
67 while (digitNum > 0) {
69 temp.multiply(ans, buBase);
70 ans.add(temp, BigUnsigned(blk[digitNum]));
75 BigUnsignedInABase::BigUnsignedInABase(const std::string &s, Base base) {
78 throw "BigUnsignedInABase(std::string, Base): The default string conversion routines use the symbol set 0-9, A-Z and therefore support only up to base 36. You tried a conversion with a base over 36; write your own string conversion routine.";
80 // This pattern is seldom seen in C++, but the analogous ``this.'' is common in Java.
83 // `s.length()' is a `size_t', while `len' is a `NumberlikeArray::Index',
84 // also known as an `unsigned int'. Some compilers warn without this cast.
85 len = Index(s.length());
88 Index digitNum, symbolNumInString;
89 for (digitNum = 0; digitNum < len; digitNum++) {
90 symbolNumInString = len - 1 - digitNum;
91 char theSymbol = s[symbolNumInString];
92 if (theSymbol >= '0' && theSymbol <= '9')
93 blk[digitNum] = theSymbol - '0';
94 else if (theSymbol >= 'A' && theSymbol <= 'Z')
95 blk[digitNum] = theSymbol - 'A' + 10;
96 else if (theSymbol >= 'a' && theSymbol <= 'z')
97 blk[digitNum] = theSymbol - 'a' + 10;
99 throw "BigUnsignedInABase(std::string, Base): Bad symbol in input. Only 0-9, A-Z, a-z are accepted.";
104 BigUnsignedInABase::operator std::string() const {
106 throw "BigUnsignedInABase ==> std::string: The default string conversion routines use the symbol set 0-9, A-Z and therefore support only up to base 36. You tried a conversion with a base over 36; write your own string conversion routine.";
108 return std::string("0");
109 char *s = new char[len + 1];
111 Index digitNum, symbolNumInString;
112 for (symbolNumInString = 0; symbolNumInString < len; symbolNumInString++) {
113 digitNum = len - 1 - symbolNumInString;
114 Digit theDigit = blk[digitNum];
116 s[symbolNumInString] = char('0' + theDigit);
118 s[symbolNumInString] = char('A' + theDigit - 10);