MegaInt – Custom data type for very large numbers

I recently created a new data type for Above the Stars. It was an interesting excursion into many things I’ve never used before so I thought I’d do a write-up regarding it.

The MegaInt is capable of holding very large numbers all in a single integer, as well displaying them in a readable format.
A basic description can be found below as well as the full script (Creative Commons 4.0).

Description

The MegaInt can hold very large numbers by splitting the 32 bits of an integer. It uses the last 24 bits for accuracy (7 significant figures) and the remaining 8 bits for power (10256).
I tried to be as descriptive as possible in the code (usually my comments are lacking) so I hope it’s at least somewhat educational, or at the very least to anyone making similar (incremental) games.

The concept itself is quite straightforward. It takes the input (converted to String) and reduces it to the 7 digit length, incrementing the power for each excess digit dropped.
I wont go into the various conversions or mathematical methods. (the latter works as normal but accounts for the powers). There may be some odd behaviours at very low numbers (always rounding up) but for the most part it works quite well.

Conclusion

Overall I’m very happy with the result. It is lightweight and accurate enough for my purposes, although there is much that could be improved.
If I were to do it again I would definitely build it to work with below-one numbers (I’d do it now but it would require a large rewrite). Some of the methods should probably be rewritten to look/run cleaner, as well. I might also focus less on saving memory and more on performance, but I’m not too sure.
Anyway, I hope you find this useful.

Code

Above is a snippet from my custom library of common functions. IsDigitsOnly extends the string class and is pretty self-explanatory. It’s used in the MegaInt script (below) when taking a string input from an outside source

Thanks for reading and have a great day!

Creative Commons License

Leave a Reply