lua-users home
lua-l archive

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


2010/12/1 Pierre-Yves Gérardy <pygy79@gmail.com>:
> Following the floating point discussion, I tried to look for a compilation
> of the various gotchas Lua beginners encounter, but I couldn't find one, so
> I decided to compile one...
> Right now, I can think of two of them:
> - Floating point inaccuracies.

Floating point behaviour is common to most programming languages, what
does this have to do with lua?

ruby:

irb(main):010:0> x = 1.1; x = x + 0.1; print("%.20f\n" % x); print(x == 1.2)
1.20000000000000017764
false=> nil

python:

>>> x = 1.1; x = x + 0.1; print("%.20f\n", x); print(x == 1.2)
('%.20f\n', 1.2000000000000002)
False

>  x = 1.1; x = x + 0.1; print(string.format("%.20f", x)); print(x == 1.2)
1.20000000000000017764
false

Never use "==" to compare float/double values is even in some C coding
standards and compiler warnings.

Cheers,
Sam