[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: bug, -0 is not evaluated correctly.
- From: Luiz Henrique de Figueiredo <lhf@...>
- Date: Thu, 6 Jan 2005 09:33:10 -0200
> There is a minor bug in the way that the constant -0 is evaluated.
I wouldn't say it's a bug. It's rather a lack of full support for IEEE 754
negative zero (a controversial feature for some).
Here is an explanation: Lua's lexer does not recognize negative constants;
it sees -2 as two tokens: - and 2, that is, as the negation of 2. When Lua
gets to generating code for this, it notices that it's trying to negate a
constant and then it fixes the sign of the constant instead of generating a
negate VM instruction (OP_UNM). Lua then adds -2 to the table of constants
of the current function. When doing so, it checks whether the constant is
already in the table. Everything is fine except when it tries to add -0 to
the table because -0 compares as equal to 0 and 0 is already in the table
of constants because it was added when the parser saw 0 (after it saw - but
before it decided that it had a negated constant).
So, short of redesigning the whole thing, I think it's hard to support -0.
Notice that even of the lexer recognized negative constants you'd still have
problems because of the optimization of the table of constants. Code like
"print(-0,0)" or "print(0,-0)" would always print "0 0".
The only way to detect negative zero is to check whether 1/(-0) is negative,
but this division is not defined in ANSI C. So, we support -0 and table
optimization. We prefer the latter...
Most people do not care for negative zero. Those that care, and know what
they're doing, can do "x=0;x=-x" as you did.