lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Sun, Mar 31, 2013 at 4:38 AM, vitaminx <vitaminx@callistix.net> wrote:
On 03/31/2013 04:27 PM, Kevin Martin wrote:
On 31 Mar 2013, at 09:06, vitaminx wrote:

With this simple one-liner I can reproduce the following arithmetic error:

for var=10,0,-0.1 do print(var) end
Are you sure this is a Lua error? Can you give a language/example where this works as you expected?

Unless using fixed/arbitrary precision, which have their own trade-offs, non-integer numbers in a computer tend to be stored in standard form in base 2. There will be a finite number of digits for both the mantissa and exponent (52 and 11 IIRC for normal Lua).

Try and write 1/10 in standard form in base 2, and you will see the problem.

Thanks,
Kev


You're right, I've tried the same in C and got "incorrect" results too.
Never had to use for loops with floats before, so I never ran into this issue.

The results are perfectly "correct" if you understand how floating point works on binary computers. Number 0.1 does not have a finite binary representation.  Every time you subtract it the errors accumulate and then the errors show up in the output.

--Leo--