Matt McCutchen's Web Site
/
bigint
/
bigint.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
I decided to delete whitespace from otherwise empty lines.
[bigint/bigint.git]
/
BigUnsignedInABase.cc
diff --git
a/BigUnsignedInABase.cc
b/BigUnsignedInABase.cc
index
a450cf0
..
b423ca2
100644
(file)
--- a/
BigUnsignedInABase.cc
+++ b/
BigUnsignedInABase.cc
@@
-1,6
+1,5
@@
/*
* Matt McCutchen's Big Integer Library
/*
* Matt McCutchen's Big Integer Library
-* http://mysite.verizon.net/mccutchen/bigint/
*/
/*
*/
/*
@@
-29,22
+28,24
@@
namespace {
}
BigUnsignedInABase::BigUnsignedInABase(const BigUnsigned &x, Base base) {
}
BigUnsignedInABase::BigUnsignedInABase(const BigUnsigned &x, Base base) {
+
// Check the base
if (base < 2)
throw "BigUnsignedInABase(BigUnsigned, Base): The base must be at least 2";
// Save the base.
// This pattern is seldom seen in C++, but the analogous ``this.'' is common in Java.
this->base = base;
// Check the base
if (base < 2)
throw "BigUnsignedInABase(BigUnsigned, Base): The base must be at least 2";
// Save the base.
// This pattern is seldom seen in C++, but the analogous ``this.'' is common in Java.
this->base = base;
-
+
// Get an upper bound on how much space we need
// Get an upper bound on how much space we need
- int maxBitLenOfX = x.getLength() *
8 * sizeof(BigUnsigned::Blk)
;
+ int maxBitLenOfX = x.getLength() *
BigUnsigned::N
;
int minBitsPerDigit = bitLen(base) - 1;
int maxDigitLenOfX = ceilingDiv(maxBitLenOfX, minBitsPerDigit);
int minBitsPerDigit = bitLen(base) - 1;
int maxDigitLenOfX = ceilingDiv(maxBitLenOfX, minBitsPerDigit);
- allocate(maxDigitLenOfX); // Get the space
-
+ len = maxDigitLenOfX; // Another change to comply with `staying in bounds'; see `BigUnsigned::divideWithRemainder'.
+ allocate(len); // Get the space
+
BigUnsigned x2(x), buBase(base);
Index digitNum = 0;
BigUnsigned x2(x), buBase(base);
Index digitNum = 0;
-
+
while (!x2.isZero()) {
// Get last digit. This is like `lastDigit = x2 % buBase, x2 /= buBase'.
BigUnsigned lastDigit(x2);
while (!x2.isZero()) {
// Get last digit. This is like `lastDigit = x2 % buBase, x2 /= buBase'.
BigUnsigned lastDigit(x2);
@@
-54,8
+55,8
@@
BigUnsignedInABase::BigUnsignedInABase(const BigUnsigned &x, Base base) {
// Move on. We can't run out of room: we figured it out above.
digitNum++;
}
// Move on. We can't run out of room: we figured it out above.
digitNum++;
}
-
- // Save the
even
tual length.
+
+ // Save the
ac
tual length.
len = digitNum;
}
len = digitNum;
}
@@
-77,10
+78,12
@@
BigUnsignedInABase::BigUnsignedInABase(const std::string &s, Base base) {
// Save the base.
// This pattern is seldom seen in C++, but the analogous ``this.'' is common in Java.
this->base = base;
// Save the base.
// This pattern is seldom seen in C++, but the analogous ``this.'' is common in Java.
this->base = base;
-
- len = s.length();
+
+ // `s.length()' is a `size_t', while `len' is a `NumberlikeArray::Index',
+ // also known as an `unsigned int'. Some compilers warn without this cast.
+ len = Index(s.length());
allocate(len);
allocate(len);
-
+
Index digitNum, symbolNumInString;
for (digitNum = 0; digitNum < len; digitNum++) {
symbolNumInString = len - 1 - digitNum;
Index digitNum, symbolNumInString;
for (digitNum = 0; digitNum < len; digitNum++) {
symbolNumInString = len - 1 - digitNum;
@@
-113,5
+116,8
@@
BigUnsignedInABase::operator std::string() const {
else
s[symbolNumInString] = char('A' + theDigit - 10);
}
else
s[symbolNumInString] = char('A' + theDigit - 10);
}
- return std::string(s);
+ std::string s2(s);
+ // 2006.05.03: This needs to be [] to match the allocation
+ delete [] s;
+ return s2;
}
}