-
Notifications
You must be signed in to change notification settings - Fork 132
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
Add convert_to_base_64 function to convert string integer to magnitudes #34
base: base-2-to-the-64
Are you sure you want to change the base?
Add convert_to_base_64 function to convert string integer to magnitudes #34
Conversation
7b6df13
to
522f8d7
Compare
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.
Thanks, but the logic you've used for conversion isn't right.
test/functions/utility.test.cpp
Outdated
|
||
TEST_CASE("Conversion to Base64 magnitudes from String", "[Utility][Helper][Base64]") { | ||
std::string num = "481482486096808911670147153572883801"; | ||
// Magnitude should be {11670147153572883801, 4814824860968089} |
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.
The magnitude is incorrect. It should be {5717256727323176281, 26101217871994095}
, since
481482486096808911670147153572883801 = 5717256727323176281⋅(264)0 + 26101217871994095⋅(264)1
include/functions/utility.hpp
Outdated
try { | ||
std::string num_slice = num.substr(start_it, stop_it); | ||
component = std::stoull(num_slice); | ||
} catch (std::out_of_range& e) { //Using out_of_range to determine overflow |
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.
This logic wouldn't work. What we want is to divide the number by the largest power of 264 that gives a non-zero quotient, push that in an array, use the remainder as the new number and repeat the process with decreasing powers until we reach 1 (0th power). Then return the reversed array.
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.
I was wondering how can we divide a number even with 264, the dividend will anyway overflow if we are going to do these in the traditional way. So i am thinking it must all be done in strings rather than converting the numbers into integer chunks or something related.
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.
Yes, we'll need to do string-based division first to convert to base 264. I'm working on removing as much code as possible while still having string division and remainder work. But it'll take some time as I'm busy with exams currently. In the meanwhile, if you have another suggestion, or would like to work on this then let me know.
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.
Yes, i will modify it to use string divisions instead.
f5c8b64
to
783828f
Compare
783828f
to
2bc7e68
Compare
magnitude = convert_to_base_2_to_the_64(num_magnitude); | ||
*/ | ||
magnitude = {0}; | ||
magnitude = convert_to_base_64(num_magnitude); |
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.
It seems we have a circular dependency here, because in order to get magnitudes we first need to create a BigInt from string and then get magnitudes by calling a function which returns a magnitude vector, in my case BigInt::to_base64
. What are your thoughts on this?
Added a function to convert
BigInt
string representation into Base64 magnitudes. It starts from back of string and checks for overflow if saved into a 64-bit variable, which means we have reached at a point in string which is a component inmagnitude
vector, push this component into the vector and then start over again from the current point in string to check for next component.