Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to get around number inaccuracy?

Asked by
2eggnog 981 Moderation Voter
9 years ago

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!

0
Could you explain why you need these usually rather insignificant digits? Legojoker 345 — 9y
0
I'm trying to make a relativistic physics simulator. Once objects get very close to the speed of light, certain properties become very sensitive to incredibly small changes in velocity. For this reason, due to the inaccuracy of the values being stored and manipulated, I get bad results that can occasionally ruin the simulation. (Singularities form, objects break the speed of light, etc.) 2eggnog 981 — 9y

2 answers

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

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.

1
*ahem* it's not 59, it's 590. 59 is 059 but there's no 0 there in pi :P blocco 185 — 9y
Ad
Log in to vote
-1
Answered by 9 years ago

I would give it a try. Be positive

0
Another big reason I want to avoid using the array approach is that fractional exponentiation will be very difficult and slow to calculate. If that's really the only viable solution, I guess I'll just have to stick with it. 2eggnog 981 — 9y

Answer this question