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 6 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
91 changes: 89 additions & 2 deletions big-int/src/operators/multiply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,97 @@

#include <istream>
#include <ostream>

#include "../bigint.hpp"
#include <math.h>
ayaankhan98 marked this conversation as resolved.
Show resolved Hide resolved
#include <bigint.hpp>
#include <algorithm>

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;
largeInt Answer = x;
for(k = 0; k < factor; k++) {
ayaankhan98 marked this conversation as resolved.
Show resolved Hide resolved
Answer.number.push_back('0');
}

return Answer;
};

auto simple_multiplication = [](const largeInt &x, const largeInt &y) {
largeInt x1 = x;
largeInt x2 = y;
largeInt adder;
largeInt Answer(0);

if(x1.number.length() == 0 || x2.number.length() == 0) {
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) {
ayaankhan98 marked this conversation as resolved.
Show resolved Hide resolved
temp = (*i - 48) * (*j - 48) + carry;
ayaankhan98 marked this conversation as resolved.
Show resolved Hide resolved
carry = temp/10;
adder.number.push_back(temp%10 + 48);
}
if(carry) {
adder.number.push_back(carry + 48);
carry = 0;
}
std::reverse(adder.number.begin(), adder.number.end());
//std::cout<<"Before Adder = "<<adder<<std::endl;
for(k=f; k != 1; k/=10) {
adder.number.push_back('0');
}
//std::cout<<"adder = "<<adder<<"Answer = "<<Answer<<"\n";
Answer = Answer + adder;
adder = largeInt();
f *= 10;
}

if(x1.sign != x2.sign)
Answer.sign = false;
ayaankhan98 marked this conversation as resolved.
Show resolved Hide resolved

return Answer;
};

largeInt x = *this;
largeInt y = next_number;

if(x == largeInt("0") || y == largeInt("0") || x.number.length() == 0 || y.number.length() == 0) {
return largeInt("0");
}
else if(x.number.length() == 1 || y.number.length() == 1) {
return simple_multiplication(x, y);
}

const int lower = std::min(x.number.length(), y.number.length());
const int higher = std::max(x.number.length(), y.number.length());

const int f = (higher >= 2*lower) ? lower:higher/2;

largeInt x1 = (f == x.number.length()) ? largeInt():largeInt(x.number.substr(0, x.number.length() - f));
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;

largeInt x1y1 = simple_multiplication(x1, y1);
largeInt x2y2 = simple_multiplication(x2, y2);
largeInt x3y3 = simple_multiplication(x3, y3);

largeInt xy = append_zeroes(x1y1, 2*f) + append_zeroes((x3y3 - x1y1 - x2y2), f) + x2y2;

if(x.sign != y.sign)
xy.sign = false;
ayaankhan98 marked this conversation as resolved.
Show resolved Hide resolved

return xy;
}


} // namespace libbig
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ foreach(testsourcefile ${TEST_SOURCES})
string ( REPLACE ".cpp" "" testname ${testsourcefile} )
add_executable( ${testname} ${testsourcefile} )
target_link_libraries( ${testname} big-int )
endforeach()
endforeach()
2 changes: 1 addition & 1 deletion tests/operators/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ foreach(testsourcefile ${TEST_SOURCES})
add_test(${testname} ${testname})
endforeach()

file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/input.txt DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/)
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/input.txt DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
45 changes: 45 additions & 0 deletions tests/operators/multiply_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <bigint.hpp>
#include <iostream>
#include <cassert>

int main()
{
/// Tests for multiplication of largeInt to largeInt (largeInt * largeInt)
/// Test 1
libbig::largeInt a("1");
libbig::largeInt b("1");
libbig::largeInt c = a * b;
assert(c == libbig::largeInt("1"));

/// Test 2
a = libbig::largeInt("999");
b = libbig::largeInt("99999");
c = a * b;
assert(c == libbig::largeInt("99899001"));

/// Test 3
a = libbig::largeInt("32516889472103571029388908423975");
b = libbig::largeInt("0");
c = a * b;
assert(c == libbig::largeInt("0"));

/// Test 4
a = libbig::largeInt("6666666666666");
b = libbig::largeInt("58723619378");
c = a * b;
assert(c == libbig::largeInt("391490795853294184253748"));

// Test 5
a = libbig::largeInt("33");
b = libbig::largeInt("-2");
c = a * b;
assert(c == libbig::largeInt("-66"));

// Test 6
a = libbig::largeInt("78239823648904375");
b = libbig::largeInt("1273898653482713986555");
c = a * b;
assert(c == libbig::largeInt("99669583064888522730377914524230678125"));

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