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

• Subject: Re: Lua minus null (-0)
• From: Dirk Laurie <dpl@...>
• Date: Thu, 9 Jun 2011 10:59:58 +0200

```On Thu, Jun 09, 2011 at 08:54:18AM +0200, David Kastrup wrote:
> 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.
>
That's in PiL, which (even though written by a Lua designer) is not
part of the standard Lua documentation.

The reference manual states only that
a % b == a - math.floor(a/b)*b

> 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!
This is a telling example.  I'll take it into account later.

> 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.
Maybe _because_ it's hard to distinguish them otherwise? :-)
Anyway, this is a question to ask of the people who define
the behaviour of C formatting.

Returning to the fact that assigning something to a[0] changes the
value of a[-0], this is indeed behaviour that should be either changed
or documented.  Changing the coding of the Lua core to accommodate
a particular number representation, when the manual itself mentions
the possibility of building Lua with a different one, seems to go
too far for something that is no more than a minor annoyance.

The following additions to the Lua 5.2 manual would IMHO be adequate:

3.4.3 – Relational Operators
@ If both arguments are numbers, then they are compared as such.
+ Note that -0 and +0, though distinguishable on almost all modern
+ computers, compare as equal.

3.4.8 – Table Constructors
@ Each field of the form [exp1] = exp2 adds to the new table an
@ entry with key exp1 and value exp2.
+ [-0] and [0] refer to the same table entry, but the form first
+ used is the actual key.

Dirk

```