tags:

views:

103

answers:

4

I am using Lua 5.1

print(10.08 - 10.07)

Rather than printing 0.01, above prints 0.0099999999999998.

Any idea how to get 0.01 form this subtraction?

+1  A: 

Use an arbitrary precision library.

Ignacio Vazquez-Abrams
If you need one. This is highly dependent on the application.
Matthew Flaschen
+2  A: 

Use Lua's string.format function:

print(string.format('%.02f', 10.08 - 10.07))
Greg Hewgill
+9  A: 

You got 0.01 from the subtraction. It is just in the form of a repeating decimal with a tiny amount of lost precision.

Lua uses the C type double to represent numbers. This is, on nearly every platform you will use, a 64-bit binary floating point value with approximately 23 decimal digits of precision. However, no amount of precision is sufficient to represent 0.01 exactly in binary. The situation is similar to attempting to write 1/3 in decimal.

Furthermore, you are subtracting two values that are very similar in magnitude. That all by itself causes an additional loss of precision.

The solution depends on what your context is. If you are doing accounting, then I would strongly recommend that you not use floating point values to represent account values because these small errors will accumulate and eventually whole cents (or worse) will appear or disappear. It is much better to store accounts in integer cents, and divide by 100 for display.

In general, the answer is to be aware of the issues that are inherent to floating point, and one of them is this sort of small loss of precision. It is easily handled by rounding answers to an appropriate number of digits for display, and never comparing results of calculations for equality.

Some resources for background:

Edit: Added the WECSSKAFPA document after all. It really is worth the study, although it will likely seem a bit overwhelming on the first pass. In the same vein, Knuth Volume 2 has extensive coverage of arithmetic in general and a large section on floating point. And, since lhf reminded me of its existence, I inserted the Lua wiki explanation of why floating point is ok to use as the sole numeric type as the first item on the list.

RBerteig
+1 very good answer. I'd also like to add one more reference if OP feels like digging in - "What every computer scientist should know about floating point numbers" at http://docs.sun.com/source/806-3568/ncg_goldberg.html
sbk
I flipped a coin and decided to leave it off the list... it is a tad overwhelming on first reading. But I'd absolutely recommend reading and understanding it as well, so I'll edit the link in where it is more visible.
RBerteig
+1  A: 

Use a Decimal Number Library for Lua.

Doug Currie