I'm currently trying to do something that requires very precise math - up to dozens of significant figures of accuracy. Unfortunately, due to obvious reasons, the numbers become inaccurate once there are too many digits. I've considered just storing numbers as arrays of digits, but I feel like that's a clumsy solution.
Just to clarify, I know why this happens. I want to see if anyone has a cleaner solution to this problem, or if someone knows of a pre-existing method for storing numbers accurately.
Thanks for any input!
Lua uses doubles for all numerals.
These are 64 bit numbers. One bit is used for sign, eleven for the exponent, and fifty-three for the fraction.
This means you have approximately 16 decimal-digits of accuracy. This should be enough for most simulations.
Note that certain operations will cause more loss-of-precision that others. There are algorithms or choices that you can make in the order of computations which minimize these. For example, Kahan summation.
If you need higher precision numbers, it would most likely be best to use floating point still as opposed to fixed point. A floating point number is stored in a scientific-number type form. This allows numbers to have a constant number of significant figures despite how big / small they are.
For efficiency purposes, rather than storing a list of digits (base 10), it would be better to use a much larger base (e.g., base 1000, or bigger), and so 10^6 * pi would be stored as something like:
{2, 3, 141, 592, 653, 590}
to represent 3 * b ^ 2 + 141 * b ^ 1 + 592 * b ^ 0 + 653 * b ^ (-1)
, etc. (where b
is 1000).
You, of course, remain to implement addition, multiplication, and division efficiently on these numbers.
I would give it a try. Be positive