Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Multiply Operator (*) overload with tests #59

Merged
merged 11 commits into from
Oct 22, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 36 additions & 18 deletions big-int/src/operators/multiply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,38 @@ namespace libbig
{
largeInt largeInt::operator*(const largeInt &next_number) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still two overloads are missing.
for largeInt * int and largeInt * int64_t (int64_t aka long long)


auto append_zeroes = [](const largeInt &x, const int factor) {
int k;
using Iterator = std::string::reverse_iterator;
ayaankhan98 marked this conversation as resolved.
Show resolved Hide resolved

auto remove_zeroes = [] (const largeInt &x) {
largeInt Answer = x;
for(k = 0; k < factor; k++) {
std::reverse(Answer.number.begin(), Answer.number.end());
for(Iterator j=Answer.number.rbegin(); j != Answer.number.rend() - 1; ++j) {
if((*j) == '0') {
Answer.number.pop_back();
}
else {
break;
}
}
std::reverse(Answer.number.begin(), Answer.number.end());
ayaankhan98 marked this conversation as resolved.
Show resolved Hide resolved
return Answer;
};

auto append_zeroes = [] (const largeInt &x, const int factor) {
largeInt Answer = x;
for(int k = 0; k < factor; k++) {
Answer.number.push_back('0');
}

return Answer;
};

auto simple_multiplication = [](const largeInt &x, const largeInt &y) {
auto simple_multiplication = [] (const largeInt &x, const largeInt &y) {

auto char_int_converter = [] (const char &x) {
ayaankhan98 marked this conversation as resolved.
Show resolved Hide resolved
return ((x >= '0' && x <= '9') ? x - '0' : x + '0') ;
};

largeInt x1 = x;
largeInt x2 = y;
largeInt adder;
Expand All @@ -55,27 +76,24 @@ namespace libbig
return largeInt();
}

int carry { 0 }, temp, f=1, k;
std::string::reverse_iterator i, j;
for(i=x1.number.rbegin(); i!=x1.number.rend(); ++i) {
for(j=x2.number.rbegin(); j!=x2.number.rend(); ++j) {
temp = (*i - 48) * (*j - 48) + carry;
int carry { 0 }, temp, f=0;
for(Iterator i=x1.number.rbegin(); i!=x1.number.rend(); ++i) {
for(Iterator j=x2.number.rbegin(); j!=x2.number.rend(); ++j) {
temp = char_int_converter(*i) * char_int_converter(*j) + carry;
carry = temp/10;
adder.number.push_back(temp%10 + 48);
adder.number.push_back(char_int_converter(temp%10));
}
if(carry) {
adder.number.push_back(carry + 48);
adder.number.push_back(char_int_converter(carry));
carry = 0;
}
std::reverse(adder.number.begin(), adder.number.end());
//std::cout<<"Before Adder = "<<adder<<std::endl;
for(k=f; k != 1; k/=10) {
for(int k=1; k <= f; ++k) {
adder.number.push_back('0');
}
//std::cout<<"adder = "<<adder<<"Answer = "<<Answer<<"\n";
Answer = Answer + adder;
adder = largeInt();
f *= 10;
f += 1;
}

if(x1.sign != x2.sign)
Expand All @@ -87,7 +105,7 @@ namespace libbig
largeInt x = *this;
largeInt y = next_number;

if(x == largeInt("0") || y == largeInt("0") || x.number.length() == 0 || y.number.length() == 0) {
if(x.number == "0" || y.number == "0" || x.number.length() == 0 || y.number.length() == 0) {
return largeInt("0");
}
else if(x.number.length() == 1 || y.number.length() == 1) {
Expand All @@ -103,7 +121,7 @@ namespace libbig
largeInt y1 = (f == y.number.length()) ? largeInt():largeInt(y.number.substr(0, y.number.length() - f));
largeInt x2 = (f == x.number.length()) ? x:largeInt(x.number.substr(x.number.length() - f, x.number.length()));
largeInt y2 = (f == y.number.length()) ? y:largeInt(y.number.substr(y.number.length() - f, y.number.length()));

const largeInt x3 = x1 + x2;
const largeInt y3 = y1 + y2;

Expand All @@ -116,7 +134,7 @@ namespace libbig
if(x.sign != y.sign)
xy.sign = false;
ayaankhan98 marked this conversation as resolved.
Show resolved Hide resolved

return xy;
return remove_zeroes(xy);
}


Expand Down
32 changes: 31 additions & 1 deletion tests/operators/multiply_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,37 @@ int main()
a = libbig::largeInt("78239823648904375");
b = libbig::largeInt("1273898653482713986555");
c = a * b;
assert(c == libbig::largeInt("99669583064888522730377914524230678125"));
assert(c == libbig::largeInt("99669605995064285419229672514230678125"));

// Test 7
a = libbig::largeInt("-0");
b = libbig::largeInt("100");
c = a * b;
assert(c == libbig::largeInt("0"));

// Test 8
a = libbig::largeInt("-100");
b = libbig::largeInt("-100");
c = a * b;
assert(c == libbig::largeInt("10000"));

// Test 9
a = libbig::largeInt("0000000000000000000000000000000000000000000000000000");
b = libbig::largeInt("1111111111111111111111111111111111111111111111111111");
c = a * b;
assert(c == libbig::largeInt("0"));

// Test 10
a = libbig::largeInt("746767836578647567834567834657836587");
b = libbig::largeInt("89275924758922476024759846938475804579247593468034769835763597028476937");
c = a * b;
assert(c == libbig::largeInt("66668389190778655840704160824409791548824154145813767587653936294222881640592188804856387839101068044294019"));

// Test 10
a = libbig::largeInt("8492387957349593498354782359782345782367895623478567894235692347856789234789236789342967891235627348589234567892345234567892369287345");
b = libbig::largeInt("824358293475908745902350926589023459023485089347892650234650273456207349236502347524357832456789235623489759235623789456782394567892359478235239423569782356234659837562378965278956237489235");
c = a * b;
assert(c == libbig::largeInt("7000770444056069403046119404826740387252166452093943636486324185225836788920060077258109995450330997820727632474615791121779806923541276193853962113432983583426568371447855101334504301369704676060587738461922443343618487980096828486083202559107333726983966552151422667448209619250487990929538296732965889159956209059231075"));

ayaankhan98 marked this conversation as resolved.
Show resolved Hide resolved
return 0;
}