how to do very large numbers?

Blitz3D Forums/Blitz3D Programming/how to do very large numbers?

stanrol(Posted 2012) [#1]
how to do very large numbers?


PowerPC603(Posted 2012) [#2]
I gues you mean numbers over 2.000.000.000? Too large to fit in an integer variable?

I was wondering about that as well.

One way:
Use 2 integer variables to create a fake 64-bit variable.
Use the first to store anything below 1.000.000.000.
Use the second to hold the multiplier for 1.000.000.000.

Example: second var = 15, first var = 950.478.562
If you combine these, you should get 15.950.478.562 (Print Var2 + "" + Var1). The "" is to split both values instead of adding them together before printing.

You also need extra functions for adding and substracting values from your self-made 64-bit variable.

Limitations: you cannot add more than 2.000.000.000 at once, since you can only pass integer variables (B3D doesn't have 64-bit vars).



Second way: use a string to hold your value.
Each digit needs it's own spot (character-index) in the string.

For this, you also need your own functions for adding, substracting, ... values stored in this string.
Using this approach, you're not limited to values of 2.000.000.000, as a string can be as large as you need it.
Your functions could use strings as parameters instead of integers (which would put the same limit on your values).

There may be more and better ways to do this.

Last edited 2012


Yasha(Posted 2012) [#3]
There's an example of string maths in action here: megacalculs

Unfortunately it's not packaged as a function library, so it might be hard to integrate with other code. It's also ridiculously slow, which is probably not the fault of the algorithms, so much as that manipulating strings in B3D is ridiculously slow anyway.

I'm sure I saw another one that used functions, but I can't find it now.

Warpy wrote a bignum library for BlitzMax: Arbitrary precision integers

If you're feeling adventurous you could try to convert it... it must be good if it's by Warpy.

If you're feeling very adventurous you could try wrapping GMP, which is probably the fastest general-purpose bignum library, but requires you to use rather awkward code.


The simplest solution is to find a scripting language that provides infinite-precision numbers and just have your math run through that as eval-strings. Most Lisps and Schemes are good for this (sadly not the TinyScheme linked in my signature)... a few other languages should too. The big advantage of doing this is that the scripting engine can accept your computation as a string and return a string, avoiding annoying data types, and can usually rely on its own internal GC to clean up the temporary values so your code can stay very simple. This solution will be somewhere between string-maths and GMP in terms of performance, but should be good enough.

Finally... how badly do you really need big numbers? What are you trying to achieve? Be aware that no matter what method you find to do the computation, there is no way to have B3D actually use the result as anything other than a string, and there is no way (even with GMP) to make the maths as fast as hardware operations.

Also, if you can live with only ~6 significant digits of precision, B3D floats actually have a range of some 3x10^38.

Last edited 2012