abacus: bignum: add log10
This commit is contained in:
parent
c730705a13
commit
31dcb91a26
|
@ -492,4 +492,22 @@ BigNum log2(BigNum const& num) {
|
|||
return res;
|
||||
}
|
||||
|
||||
BigNum log10(BigNum const& num) {
|
||||
assert(num.is_canonicalized());
|
||||
assert(BASE == 10);
|
||||
|
||||
if (num.is_zero()) {
|
||||
throw std::invalid_argument("attempt to take the log10 of zero");
|
||||
} else if (num.is_negative()) {
|
||||
throw std::invalid_argument(
|
||||
"attempt to take the log10 of a negative number");
|
||||
}
|
||||
|
||||
auto res = BigNum(num.digits_.size() - 1);
|
||||
|
||||
assert(res.is_canonicalized());
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
} // namespace abacus::bignum
|
||||
|
|
|
@ -93,6 +93,8 @@ public:
|
|||
|
||||
friend BigNum log2(BigNum const& num);
|
||||
|
||||
friend BigNum log10(BigNum const& num);
|
||||
|
||||
friend bool operator==(BigNum const& lhs, BigNum const& rhs) {
|
||||
return lhs.equal(rhs);
|
||||
}
|
||||
|
|
|
@ -397,3 +397,23 @@ TEST(BigNum, log2) {
|
|||
EXPECT_EQ(log2(eight), three);
|
||||
EXPECT_EQ(log2(nine), three);
|
||||
}
|
||||
|
||||
TEST(BigNum, log10) {
|
||||
auto const zero = BigNum(0);
|
||||
auto const one = BigNum(1);
|
||||
auto const two = BigNum(2);
|
||||
auto const nine = BigNum(9);
|
||||
auto const ten = BigNum(10);
|
||||
auto const eleven = BigNum(11);
|
||||
auto const ninety_nine = BigNum(99);
|
||||
auto const hundred = BigNum(100);
|
||||
auto const hundred_one = BigNum(101);
|
||||
|
||||
EXPECT_EQ(log10(one), zero);
|
||||
EXPECT_EQ(log10(nine), zero);
|
||||
EXPECT_EQ(log10(ten), one);
|
||||
EXPECT_EQ(log10(eleven), one);
|
||||
EXPECT_EQ(log10(ninety_nine), one);
|
||||
EXPECT_EQ(log10(hundred), two);
|
||||
EXPECT_EQ(log10(hundred_one), two);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue