From 2325fe76bc8ce2bf638da0644e83475be57639d5 Mon Sep 17 00:00:00 2001 From: Samuel Herts Date: Mon, 20 May 2024 17:10:54 -0400 Subject: [PATCH] Debugging for Modulus --- bigint.h | 25 +++++++++++++++------ tests/test.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 78 insertions(+), 8 deletions(-) diff --git a/bigint.h b/bigint.h index d3b242f..48bac96 100644 --- a/bigint.h +++ b/bigint.h @@ -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; @@ -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)) { @@ -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; } @@ -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) diff --git a/tests/test.cpp b/tests/test.cpp index bd73c71..2fb9550 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -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); +} +