-
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
Use a vector of integers instead of a string for faster computation #24
Comments
The idea of using numbers in base 2^64 seems interesting. But the representation you have used is quite opposite of what is used commonly i.e Leftmost digit is the most significant one and rightmost digit is the least significant. Is there any reason behind this choice? |
@surajumang Since all arithmetic operations (except division) are done from right to left, it's simpler to store the right-most digit as the first digit in an array since it will be processed first. But this is just to make the code simpler. When written formally, the most significant digits are always written first, followed by less significant digits to the right. |
I would like to work on this but think it will require a complete overhaul of the existing codebase like redefining all the arithmetic operations. What do you say? |
@surajumang Yes, it would require a complete rewrite of all the arithmetic operators and a few other methods. I'll be creating a new branch and will implement some basic stuff like converting from base 10 to base 264. Once that's done, I'll notify you and you can contribute to that branch. |
@SingleJourney English please! Google Translate isn't perfect :) |
Could you let me know when you've added that branch? |
@surajumang @OmriHab I've added the branch |
Can i recommend using |
Alternatively unsigned long long can be used and just take into consideration that it isn't necessarily 64 bit. |
if you are implementing any feature then probably you should create a issue and then implement that particular feature like first create issue Implement sum of BigInts in vectorized implementation and then sumbit PR for that particular issue. This way different people can work on it |
@OmriHab Good suggestion! I'll change the type to |
Is the issue still open? If it is, I will start working on it. |
Hi, I see the issue as open but seeing as its been over a year I would like to touch base and ask what I should work on as I would like to help. |
@DarkenedOrigins If you're interested in working on it then I would be glad to help you by reviewing the code and helping with the logic. You can start by creating a draft PR in which you can implement a utility function that takes a string representation of an integer and converts it to its base 2⁶⁴ integer vector representation. If anyone else is interested in working on this too then they can contribute in the same PR. |
Awesome, would you prefer that I create this in its own class called BigInt64 or i bolt it on to the current implementation |
@DarkenedOrigins The existing class can be renamed to |
inorder to be less destructive I made a new class called BigInt64. My implementation actually uses the string based BigInt internally. The only problem I had was difficulty in compiling since I have never used cmake before nor have I worked with such fragmented code where each implementation almost has its own file. Some help would be greatly appreciated. |
Good call! We can do the renaming later.
Yeah, the documentation related to development is not very good at the moment. I'll add details regarding how and when to create a new header file, how to write tests for it, and how to compile and run the tests using CMake and CTest. If you need help on anything more specific, you can leave a comment in PR #58 that you opened and @mention me in it. I'll get back to you whenever I can. |
use |
Instead of having the number's magnitude stored in a string, it would be much more efficient, in terms of both time and space, to have it stored in a vector of unsigned 64-bit integers (
std::vector<uint64_t>
). That is, store a number's digits in base 264.The following table lists how the number's magnitude will be represented in base 264:
{0}
{1}
{2}
{18446744073709551615}
{0, 1}
{1, 1}
{2, 1}
{0, 2}
{11670147153572883801, 4814824860968089}
{0, 0, 1}
{0, 0, 0, 1}
{0, 0, 0, 0, 1}
The way how it could speed up performance is that, for example, when adding two
BigInt
s, instead of adding their corresponding decimal digits together, their corresponding base 264 digits will be added, each digit in a single operation. Moreover, this addition (or any other arithmetic operation) of vectors can be vectorised by using SSE or AVX, which would further improve performance by a few folds.Say, for instance, that you were adding two 5000 digit numbers. In base 10 they have 5000 digits, which means that it would take 5000 operations to add them together. Instead, if they were in base 264, they would have 260 digits, and would therefore require only 260 operations. Using AVX-512, vectors with up to 8 64-bit digits can be added together in a single operation. Which means that by using vectorized operations, two 5000 digit numbers could be added using just 33 operations.
The text was updated successfully, but these errors were encountered: