lua-users home
lua-l archive

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


> ------------------------------
> Date: Wed, 1 Dec 2010 12:54:46 -0800
> From: Sam Roberts <vieuxtech@gmail.com>
> Subject: Re: Documenting Lua gotchas for newbies.
> To: Lua mailing list <lua-l@lists.lua.org>
> Message-ID:
> 	<AANLkTikHB9qe9iHZgxGpGg+j6B-nP6yZThSjyghCTvC_@mail.gmail.com>
> Content-Type: text/plain; charset=ISO-8859-1
> 
> 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

I understand your sentiment, but FP inaccuracy is definitely a newbie
FAQ, and being that Lua is often a first language for otherwise
non-programmers, giving newbies a heads up about FP is probably a good
idea.

I'm certain there is a way to include FP inaccuracy in the list without
giving the impression that it is Lua specific. At the very least, I
would recommend placing it further down the list (though I believe the
current list is not in priority order). Better yet, have a separate
section for items which are not Lua specific, but tend to trip up
newbies w/o programming experience.

John Giors
Independent Programmer
Three Eyes Software
jgiors@ThreeEyesSoftware.com
http://ThreeEyesSoftware.com