Skip to content

Commit

Permalink
Debugging for Modulus
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Herts committed May 20, 2024
1 parent 3ccadec commit 2325fe7
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 8 deletions.
25 changes: 18 additions & 7 deletions bigint.h
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,12 @@ namespace bigint_ns {
*this = mod(std::to_string(this->base_repr), rhs.str);
}
} else {
*this = mod(this->str, rhs.str);
if (!rhs.is_big) {
*this = mod(this->str, std::to_string(rhs.base_repr));
}
else {
*this = mod(this->str, rhs.str);
}
}

return *this;
Expand Down Expand Up @@ -406,7 +411,13 @@ namespace bigint_ns {
return lhs.base_repr < rhs.base_repr;
}
// TODO: Implement half
return std::to_string(lhs.base_repr) < rhs.str;
auto temp = std::to_string(lhs.base_repr);
if(temp.length() == rhs.str.length())
{
return temp < rhs.str;
}

return temp.length() < rhs.str.length();
}

if (is_negative(lhs) && is_negative(rhs)) {
Expand Down Expand Up @@ -473,7 +484,7 @@ namespace bigint_ns {
return std::abs(s.base_repr);
}
if (is_negative(s))
return s.str.substr(1, s.str.length() - 2);
return s.str.substr(1, s.str.length() - 1);

return s;
}
Expand Down Expand Up @@ -746,22 +757,22 @@ namespace bigint_ns {
// than divisor.
int idx = 0;
bigint temp = char_to_int(numerator.str[idx]);
while (idx < (numerator.str.size() - 1) && temp < denominator)
while (idx < (numerator.str.size() - 1) && temp < denominator) {
temp = temp * 10 + (char_to_int(numerator.str[++idx]));

}
// Repeatedly divide divisor with temp. After
// every division, update temp to include one
// more digit.
while ((numerator.str.size() - 1) > idx)
{
// Store result in answer i.e. temp / divisor
ans += int_to_char(int(temp / denominator));
ans += int_to_char(int(temp / int(denominator)));

// Take next digit of number
temp = char_to_int(int((temp % denominator) * 10 + numerator.str[++idx]));
}

ans += int_to_char(int(temp / denominator));
ans += int_to_char(int(temp / int(denominator)));

// If divisor is greater than number
if (ans.length() == 0)
Expand Down
61 changes: 60 additions & 1 deletion tests/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,69 @@ TEST(BigInt_test, Division_Tests)
bigint small_number = 9955;
bigint huge_number_1 = "123456789";
bigint huge_number_2 = "9999999999999999999";
//ASSERT_EQ(bigint("30") / bigint("20"), "1");
ASSERT_EQ(bigint("30") / bigint("20"), "1");
ASSERT_EQ(small_number / 5, 1991);
ASSERT_EQ(small_number / 181, 55);
ASSERT_EQ(huge_number_1 / 2, 61728394);
ASSERT_EQ(huge_number_2 / huge_number_1, 81000000737);
}

TEST(BigInt_test, Comparison_Tests)
{
bigint small_number = 9955;
bigint huge_number_1 = "123456789";
bigint huge_number_2 = "9999999999999999999";
ASSERT_TRUE(small_number > 5);
ASSERT_TRUE(small_number > 0);
ASSERT_TRUE(small_number > -10);
ASSERT_TRUE(small_number > "-123456789");
ASSERT_FALSE(small_number > huge_number_1);

ASSERT_TRUE(small_number < huge_number_1);
ASSERT_TRUE(small_number < huge_number_2);
ASSERT_TRUE(huge_number_1 < huge_number_2);

ASSERT_TRUE(small_number == small_number);
ASSERT_TRUE(small_number >= small_number);
ASSERT_TRUE(small_number <= small_number);
ASSERT_TRUE(huge_number_2 == huge_number_2);
ASSERT_TRUE(huge_number_2 >= huge_number_2);
ASSERT_TRUE(huge_number_2 <= huge_number_2);

ASSERT_TRUE(huge_number_2 > 0);
ASSERT_TRUE(huge_number_2 > "0");
ASSERT_TRUE(huge_number_2 > "-1");

ASSERT_TRUE(bigint(0) == 0);
ASSERT_TRUE(bigint(0) >= 0);
ASSERT_TRUE(bigint(0) <= 0);
ASSERT_TRUE(bigint(0) == "0");
ASSERT_TRUE(bigint(0) >= "0");
ASSERT_TRUE(bigint(0) <= "0");

ASSERT_TRUE(bigint(1) == 1);
ASSERT_TRUE(bigint(1) >= 1);
ASSERT_TRUE(bigint(1) <= 1);
ASSERT_TRUE(bigint(1) == "1");
ASSERT_TRUE(bigint(1) >= "1");
ASSERT_TRUE(bigint(1) <= "1");
}

TEST(BigInt_test, Modulus_Tests)
{
bigint small_number = 9955;
bigint huge_number_1 = "123456789";
bigint huge_number_2 = "9999999999999999999";
ASSERT_EQ(small_number % 2, 1);
ASSERT_EQ(small_number % 3, 1);
ASSERT_EQ(small_number % 4, 3);
ASSERT_EQ(small_number % 5, 0);

ASSERT_EQ(huge_number_1 % 2, 1);
ASSERT_EQ(huge_number_1 % 3, 0);
ASSERT_EQ(huge_number_1 % 4, 1);
ASSERT_EQ(huge_number_1 % 5, 4);

ASSERT_EQ(huge_number_1 % small_number, 4834);
}

0 comments on commit 2325fe7

Please sign in to comment.