-
Notifications
You must be signed in to change notification settings - Fork 131
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
base: base-2-to-the-64
Are you sure you want to change the base?
Changes from 5 commits
282c24d
2a466d2
263dc01
d5a6fe3
b0844cc
17902e4
8401447
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,21 +16,21 @@ | |
------------------- | ||
*/ | ||
|
||
BigInt::BigInt() { | ||
magnitude = { 0 }; | ||
is_negative = false; | ||
} | ||
BigInt::BigInt() | ||
: magnitude(1,0) | ||
, is_negative(false) | ||
{ } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines shouldn't start with a grammar symbol (like commas and colons): 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) | ||
{ } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same suggestion as above. |
||
|
||
|
||
/* | ||
|
@@ -39,7 +39,15 @@ BigInt::BigInt(const BigInt& num) { | |
*/ | ||
|
||
BigInt::BigInt(const long long& num) { | ||
magnitude = { (unsigned long long) llabs(num) }; | ||
if (sizeof(long long) == (sizeof(uint64_t))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of these checks at multiple places, I suggest we change all occurrences of |
||
magnitude = { (uint64_t) llabs(num) }; | ||
|
||
// If size not equal, long long will be bigger | ||
else { | ||
magnitude.push_back((uint64_t)llabs((uint64_t)num)); | ||
if (num / UINT64_MAX > 0) | ||
magnitude.push_back((uint64_t)llabs(num / UINT64_MAX)); | ||
} | ||
is_negative = num < 0; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,8 +15,16 @@ | |
*/ | ||
|
||
std::string BigInt::to_string() const { | ||
// prefix with sign if negative | ||
return this->sign == '-' ? "-" + this->value : this->value; | ||
|
||
std::string num_string; | ||
|
||
for (uint64_t ull : this->magnitude) | ||
while (ull > 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This wouldn't work. Base conversion will need to be done. I'm working on it, so you can leave this as it previously was. |
||
num_string += ((ull % 10) + '0'); | ||
ull /= 10; | ||
} | ||
|
||
return this->is_negative ? "-" + num_string : num_string; | ||
} | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,8 @@ | |
#define BIG_INT_UTILITY_FUNCTIONS_HPP | ||
|
||
#include <tuple> | ||
#include <stdint.h> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please change this to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct me if i'm wrong, would that require every reference to uint64_t to be prefixed with std::? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because in other c++ code that I've seen they don't use the std:: prefix There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, you wouldn't need the |
||
#include <limits> | ||
|
||
|
||
/* | ||
|
@@ -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 | ||
---------------------- | ||
|
@@ -110,4 +111,78 @@ 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) { | ||
std::vector<uint64_t> tmp(num_zeroes, 0); | ||
num.insert(num.end(), tmp.begin(), tmp.end()); | ||
} | ||
|
||
/* | ||
strip_leading_zeroes | ||
-------------------- | ||
Strip the leading zeroes from a number represented as a vector. | ||
*/ | ||
|
||
void strip_trailing_zeroes(std::vector<uint64_t>& num) { | ||
for (auto digit_itter = num.rbegin(); digit_itter != num.rend(); digit_itter++) | ||
if (*digit_itter != 0) { | ||
num = std::vector<uint64_t>(num.rend(), digit_itter + 1); | ||
return; | ||
} | ||
|
||
// If all 0's | ||
num.clear(); | ||
num.push_back(0); | ||
} | ||
|
||
/* | ||
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
} | ||
|
@@ -31,8 +31,8 @@ BigInt& BigInt::operator=(const BigInt& num) { | |
|
||
BigInt& BigInt::operator=(const long long& num) { | ||
BigInt temp(num); | ||
value = temp.value; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Eliminating the constructor call here would be a good idea. |
||
sign = temp.sign; | ||
magnitude = temp.magnitude; | ||
is_negative = temp.is_negative; | ||
|
||
return *this; | ||
} | ||
|
@@ -45,8 +45,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; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please change this to
<cstdint>
.