lua-users home
lua-l archive

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


Dirk Laurie <dpl@sun.ac.za> writes:

> On Wed, Jun 08, 2011 at 07:37:41PM +0200, Everett L Williams II wrote:
>> Zero does not have a sign in any mathematics of which I am aware. 
>
> In Calculus 101, about three days into the course, we show our
> students this:
> $$  \lim_{x→0-} |x|/x = -1; \lim_{x→0+} |x|/x = +1. $$
>
> But that is besides the point.  Floating point is a complicated
> subject and not even Calculus 101 is of much use in trying to
> understand it.  I've posted a link to David Goldberg's article
> “What every computer scientist should know about floating point
> arithmetic”.  Not only computer scientists should know what's in
> it.  It's a good way to overcome prejudices caused by ignorance.

The topic is not so much what every computer scientist should know about
floating point arithmetic, but what every computer scientist should not
need to know about floating point arithmetic when he is working purely
with integers.

Lua does not have an integral type, and the given rationale for that is
that it does not need it since the integral subset of floating point
numbers works just as well.

But statements like

    For integer arguments, [the modulo operator] has the usual meaning,
    with the result always having the same sign as the second argument

are falsified by 0%(-2) -> 0 rather than -0, while 0*(-2) does yield -0.

There is also the oddity that memoization of functions that
differentiate between +0 and -0 _fails_:

a={};a[-0]=5;a[0]=-5;for i,v in pairs(a) do print(i,v) end

yields

-0      -5

a key/value pair that has never been entered!  Since it is actually near
to impossible to differentiate between 0 and -0 programmatically except
by the rather expensive act of inverting them (unlike inf and -inf,
0 and -0 are equivalent for value comparison purposes and indexing) or
stringifying them, one really wonders what good it is to have them print
differently.  After all, floating point numbers are not printed to full
precision anyway usually.

-- 
David Kastrup