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

Updated functions to vector format #25

Open
wants to merge 7 commits into
base: base-2-to-the-64
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
39 changes: 20 additions & 19 deletions include/BigInt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,23 @@
#define BIG_INT_HPP

#include <iostream>
#include <cstdint>
#include <vector>

class BigInt {
std::vector<unsigned long long> magnitude;
std::vector<uint64_t> magnitude;
bool is_negative;

public:
// Constructors:
BigInt();
BigInt(const BigInt&);
BigInt(const long long&);
BigInt(const int64_t&);
BigInt(const std::string&);

// Assignment operators:
BigInt& operator=(const BigInt&);
BigInt& operator=(const long long&);
BigInt& operator=(const int64_t&);
BigInt& operator=(const std::string&);

// Unary arithmetic operators:
Expand All @@ -37,11 +38,11 @@ class BigInt {
BigInt operator*(const BigInt&) const;
BigInt operator/(const BigInt&) const;
BigInt operator%(const BigInt&) const;
BigInt operator+(const long long&) const;
BigInt operator-(const long long&) const;
BigInt operator*(const long long&) const;
BigInt operator/(const long long&) const;
BigInt operator%(const long long&) const;
BigInt operator+(const int64_t&) const;
BigInt operator-(const int64_t&) const;
BigInt operator*(const int64_t&) const;
BigInt operator/(const int64_t&) const;
BigInt operator%(const int64_t&) const;
BigInt operator+(const std::string&) const;
BigInt operator-(const std::string&) const;
BigInt operator*(const std::string&) const;
Expand All @@ -54,11 +55,11 @@ class BigInt {
BigInt& operator*=(const BigInt&);
BigInt& operator/=(const BigInt&);
BigInt& operator%=(const BigInt&);
BigInt& operator+=(const long long&);
BigInt& operator-=(const long long&);
BigInt& operator*=(const long long&);
BigInt& operator/=(const long long&);
BigInt& operator%=(const long long&);
BigInt& operator+=(const int64_t&);
BigInt& operator-=(const int64_t&);
BigInt& operator*=(const int64_t&);
BigInt& operator/=(const int64_t&);
BigInt& operator%=(const int64_t&);
BigInt& operator+=(const std::string&);
BigInt& operator-=(const std::string&);
BigInt& operator*=(const std::string&);
Expand All @@ -78,12 +79,12 @@ class BigInt {
bool operator>=(const BigInt&) const;
bool operator==(const BigInt&) const;
bool operator!=(const BigInt&) const;
bool operator<(const long long&) const;
bool operator>(const long long&) const;
bool operator<=(const long long&) const;
bool operator>=(const long long&) const;
bool operator==(const long long&) const;
bool operator!=(const long long&) const;
bool operator<(const int64_t&) const;
bool operator>(const int64_t&) const;
bool operator<=(const int64_t&) const;
bool operator>=(const int64_t&) const;
bool operator==(const int64_t&) const;
bool operator!=(const int64_t&) const;
bool operator<(const std::string&) const;
bool operator>(const std::string&) const;
bool operator<=(const std::string&) const;
Expand Down
18 changes: 8 additions & 10 deletions include/constructors/constructors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,28 @@
-------------------
*/

BigInt::BigInt() {
magnitude = { 0 };
is_negative = false;
}
BigInt::BigInt():
magnitude(1,0),
is_negative(false) { }


/*
Copy constructor
----------------
*/

BigInt::BigInt(const BigInt& num) {
magnitude = num.magnitude;
is_negative = num.is_negative;
}
BigInt::BigInt(const BigInt& num) :
magnitude(num.magnitude),
is_negative(num.is_negative) { }


/*
Integer to BigInt
-----------------
*/

BigInt::BigInt(const long long& num) {
magnitude = { (unsigned long long) llabs(num) };
BigInt::BigInt(const int64_t& num) {
magnitude = { (uint64_t) llabs(num) };
is_negative = num < 0;
}

Expand Down
8 changes: 6 additions & 2 deletions include/functions/conversion.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@
*/

std::string BigInt::to_string() const {
// prefix with sign if negative
return this->sign == '-' ? "-" + this->value : this->value;
/*
TODO
----
Convert to string
*/
return "";
}


Expand Down
10 changes: 5 additions & 5 deletions include/functions/math.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ BigInt pow(const BigInt& base, int exp) {
Returns a BigInt equal to base^exp.
*/

BigInt pow(const long long& base, int exp) {
BigInt pow(const int64_t& base, int exp) {
return pow(BigInt(base), exp);
}

Expand Down Expand Up @@ -159,7 +159,7 @@ BigInt gcd(const BigInt &num1, const BigInt &num2){
--------------------
*/

BigInt gcd(const BigInt& num1, const long long& num2){
BigInt gcd(const BigInt& num1, const int64_t& num2){
return gcd(num1, BigInt(num2));
}

Expand All @@ -179,7 +179,7 @@ BigInt gcd(const BigInt& num1, const std::string& num2){
--------------------
*/

BigInt gcd(const long long& num1, const BigInt& num2){
BigInt gcd(const int64_t& num1, const BigInt& num2){
return gcd(BigInt(num1), num2);
}

Expand Down Expand Up @@ -213,7 +213,7 @@ BigInt lcm(const BigInt& num1, const BigInt& num2) {
--------------------
*/

BigInt lcm(const BigInt& num1, const long long& num2){
BigInt lcm(const BigInt& num1, const int64_t& num2){
return lcm(num1, BigInt(num2));
}

Expand All @@ -233,7 +233,7 @@ BigInt lcm(const BigInt& num1, const std::string& num2){
--------------------
*/

BigInt lcm(const long long& num1, const BigInt& num2){
BigInt lcm(const int64_t& num1, const BigInt& num2){
return lcm(BigInt(num1), num2);
}

Expand Down
15 changes: 7 additions & 8 deletions include/functions/random.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,13 @@ BigInt big_random(size_t num_digits = 0) {
// use a random number for it:
num_digits = 1 + rand_generator() % MAX_RANDOM_LENGTH;

BigInt big_rand;
big_rand.value = ""; // clear value to append digits
while (big_rand.value.size() < num_digits)
big_rand.value += std::to_string(rand_generator());
if (big_rand.value.size() != num_digits)
big_rand.value.erase(num_digits); // erase extra digits

return big_rand;
std::string random_value = "";
while (random_value.size() < num_digits)
random_value += std::to_string(rand_generator());
if (random_value.size() != num_digits)
random_value.erase(num_digits); // erase extra digits

return BigInt(random_value);
}


Expand Down
71 changes: 70 additions & 1 deletion include/functions/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#define BIG_INT_UTILITY_FUNCTIONS_HPP

#include <tuple>
#include <cstdint>
#include <limits>


/*
Expand Down Expand Up @@ -93,7 +95,6 @@ std::tuple<std::string, std::string> get_larger_and_smaller(const std::string& n
return std::make_tuple(larger, smaller);
}


/*
is_power_of_10
----------------------
Expand All @@ -110,4 +111,72 @@ bool is_power_of_10(const std::string& num){
return true; // first digit is 1 and the following digits are all 0
}


/*--------------------- Vector functions ---------------------*/

/*
add_leading_zeroes
------------------
Adds a given number of leading zeroes to a string-represented integer `num`.
*/

void add_leading_zeroes(std::vector<uint64_t>& num, size_t num_zeroes) {
std::vector<uint64_t> tmp(num_zeroes, 0);
tmp.insert(tmp.end(), num.begin(), num.end());
num = tmp;
}


/*
add_trailing_zeroes
-------------------
Adds a given number of trailing zeroes to a string-represented integer `num`.
*/

void add_trailing_zeroes(std::vector<uint64_t>& num, size_t num_zeroes) {
for (size_t i = 0; i < num_zeroes; i++)
num.push_back(0);
}

/*
strip_trailing_zeroes
--------------------
Strip the trailing zeroes from a number represented as a vector.
*/

void strip_trailing_zeroes(std::vector<uint64_t>& num) {
// Do not strip the last zero if num is all zeros
while (num.size() > 1 and num[num.size() - 1] == 0)
num.pop_back();
}

/*
get_larger_and_smaller
----------------------
Identifies the given vector-represented integers as `larger` and `smaller`,
padding the smaller number with leading zeroes to make it equal in length to
the larger number.
Note: for simplicity and efficiency, this function only compares length and not value.
*/

std::tuple<std::vector<uint64_t>, std::vector<uint64_t>> get_larger_and_smaller(const std::vector<uint64_t>& num1,
const std::vector<uint64_t>& num2) {
std::vector<uint64_t> larger, smaller;
if (num1.size() > num2.size()) {
larger = num1;
smaller = num2;
}
else {
larger = num2;
smaller = num1;
}

// pad the smaller number with zeroes
add_trailing_zeroes(smaller, larger.size() - smaller.size());

return std::make_tuple(larger, smaller);
}



#endif // BIG_INT_UTILITY_FUNCTIONS_HPP
10 changes: 5 additions & 5 deletions include/operators/arithmetic_assignment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ BigInt& BigInt::operator%=(const BigInt& num) {
-----------------
*/

BigInt& BigInt::operator+=(const long long& num) {
BigInt& BigInt::operator+=(const int64_t& num) {
*this = *this + BigInt(num);

return *this;
Expand All @@ -90,7 +90,7 @@ BigInt& BigInt::operator+=(const long long& num) {
-----------------
*/

BigInt& BigInt::operator-=(const long long& num) {
BigInt& BigInt::operator-=(const int64_t& num) {
*this = *this - BigInt(num);

return *this;
Expand All @@ -102,7 +102,7 @@ BigInt& BigInt::operator-=(const long long& num) {
-----------------
*/

BigInt& BigInt::operator*=(const long long& num) {
BigInt& BigInt::operator*=(const int64_t& num) {
*this = *this * BigInt(num);

return *this;
Expand All @@ -114,7 +114,7 @@ BigInt& BigInt::operator*=(const long long& num) {
-----------------
*/

BigInt& BigInt::operator/=(const long long& num) {
BigInt& BigInt::operator/=(const int64_t& num) {
*this = *this / BigInt(num);

return *this;
Expand All @@ -126,7 +126,7 @@ BigInt& BigInt::operator/=(const long long& num) {
-----------------
*/

BigInt& BigInt::operator%=(const long long& num) {
BigInt& BigInt::operator%=(const int64_t& num) {
*this = *this % BigInt(num);

return *this;
Expand Down
15 changes: 7 additions & 8 deletions include/operators/assignment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
*/

BigInt& BigInt::operator=(const BigInt& num) {
value = num.value;
sign = num.sign;
magnitude = num.magnitude;
is_negative = num.is_negative;

return *this;
}
Expand All @@ -29,10 +29,9 @@ BigInt& BigInt::operator=(const BigInt& num) {
----------------
*/

BigInt& BigInt::operator=(const long long& num) {
BigInt temp(num);
value = temp.value;
sign = temp.sign;
BigInt& BigInt::operator=(const int64_t& num) {
magnitude = { (uint64_t) llabs(num) };
is_negative = (num < 0);

return *this;
}
Expand All @@ -45,8 +44,8 @@ BigInt& BigInt::operator=(const long long& num) {

BigInt& BigInt::operator=(const std::string& num) {
BigInt temp(num);
value = temp.value;
sign = temp.sign;
magnitude = temp.magnitude;
is_negative = temp.is_negative;

return *this;
}
Expand Down
Loading