Compare commits

..

No commits in common. "31dcb91a2666158414c28c724029543aa9069a36" and "c06444f90c22d5464f40872e3f9081ad3e4cc600" have entirely different histories.

4 changed files with 10 additions and 102 deletions

View file

@ -60,7 +60,7 @@
devShell = pkgs.mkShell {
inherit (self.defaultPackage.${system})
checkInputs
buildInputs
nativeBuildInputs
;
@ -82,12 +82,11 @@
pkg-config
];
checkInputs = with pkgs; [
buildInputs = with pkgs; [
gtest
];
meta = with pkgs.lib; {
broken = true; # Only breaks under nix
description = "A simple calculator using big numbers";
homepage = "https://gitea.belanyi.fr/ambroisie/abacus";
license = licenses.mit;

View file

@ -24,7 +24,7 @@ bool do_less_than(digits_type const& lhs, digits_type const& rhs) {
rhs.rend());
}
void trim_leading_zeros(digits_type& num) {
void do_trim_leading_zeros(digits_type& num) {
auto const it
= std::find_if(num.rbegin(), num.rend(), [](auto v) { return v != 0; });
num.erase(it.base(), num.end());
@ -82,7 +82,7 @@ digits_type do_substraction(digits_type const& lhs, digits_type const& rhs) {
std::transform(complement.begin(), complement.end(), complement.begin(),
take_complement);
trim_leading_zeros(complement);
do_trim_leading_zeros(complement);
return complement;
}
@ -150,7 +150,7 @@ digits_type do_halve(digits_type num) {
}
}
trim_leading_zeros(num);
do_trim_leading_zeros(num);
return num;
}
@ -361,7 +361,7 @@ bool BigNum::less_than(BigNum const& rhs) const {
}
void BigNum::canonicalize() {
trim_leading_zeros(digits_);
do_trim_leading_zeros(digits_);
if (digits_.size() == 0) {
sign_ = 0;
@ -413,8 +413,8 @@ std::pair<BigNum, BigNum> div_mod(BigNum const& lhs, BigNum const& rhs) {
return std::make_pair(BigNum(), BigNum());
}
auto quotient = BigNum(0);
auto remainder = BigNum(0);
BigNum quotient;
BigNum remainder;
std::tie(quotient.digits_, remainder.digits_)
= do_div_mod(lhs.digits_, rhs.digits_);
@ -439,7 +439,7 @@ BigNum pow(BigNum const& lhs, BigNum const& rhs) {
return BigNum();
}
auto res = BigNum(0);
BigNum res;
res.digits_ = do_pow(lhs.digits_, rhs.digits_);
res.sign_ = is_odd(rhs.digits_) ? lhs.sign_ : 1;
@ -458,7 +458,7 @@ BigNum sqrt(BigNum const& num) {
"attempt to take the square root of a negative number");
}
auto res = BigNum(0);
BigNum res;
res.digits_ = do_sqrt(num.digits_);
res.sign_ = 1;
@ -468,46 +468,4 @@ BigNum sqrt(BigNum const& num) {
return res;
}
BigNum log2(BigNum const& num) {
assert(num.is_canonicalized());
if (num.is_zero()) {
throw std::invalid_argument("attempt to take the log2 of zero");
} else if (num.is_negative()) {
throw std::invalid_argument(
"attempt to take the log2 of a negative number");
}
auto tmp = num;
auto res = BigNum(0);
auto one = BigNum(1);
while (tmp > one) {
tmp.digits_ = do_halve(tmp.digits_);
res += one;
}
assert(res.is_canonicalized());
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

View file

@ -91,10 +91,6 @@ public:
friend BigNum sqrt(BigNum const& num);
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);
}
@ -119,10 +115,6 @@ public:
return !(lhs < rhs);
}
explicit operator bool() {
return !is_zero();
}
bool is_zero() const;
bool is_positive() const;
bool is_negative() const;

View file

@ -376,44 +376,3 @@ TEST(BigNum, sqrt) {
EXPECT_EQ(sqrt(ninety_nine), nine);
EXPECT_EQ(sqrt(hundred), ten);
}
TEST(BigNum, log2) {
auto const zero = BigNum(0);
auto const one = BigNum(1);
auto const two = BigNum(2);
auto const three = BigNum(3);
auto const four = BigNum(4);
auto const five = BigNum(5);
auto const seven = BigNum(7);
auto const eight = BigNum(8);
auto const nine = BigNum(9);
EXPECT_EQ(log2(one), zero);
EXPECT_EQ(log2(two), one);
EXPECT_EQ(log2(three), one);
EXPECT_EQ(log2(four), two);
EXPECT_EQ(log2(five), two);
EXPECT_EQ(log2(seven), two);
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);
}