[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: tostring and 0
- From: Drake Wilson <drake@...>
- Date: Fri, 27 Apr 2012 05:20:42 -0500
Quoth Valeriy Skurikhin <keen.vs@gmail.com>, on 2012-04-27 16:57:42 +0700:
> Recently I found, that results of tostring(0) and tostring(-0) may
> depend on call order.
>
> Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
> > return (tostring(0) .. tostring(-0))
> 00
> > return (tostring(-0) .. tostring(0))
> -0-0
It looks like Lua is merging positive and negative zero in the
constant vector:
| $ cat negzero.lua
| print(tostring(0)..tostring(-0))
| print(tostring(-0)..tostring(0))
| $ lua negzero.lua
| 00
| 00
| $ luac -v
| Lua 5.1.5 Copyright (C) 1994-2012 Lua.org, PUC-Rio
| $ luac -l negzero.lua
|
| main <negzero.lua:0,0> (19 instructions, 76 bytes at 0x17ce530)
| 0+ params, 4 slots, 0 upvalues, 0 locals, 3 constants, 0 functions
| 1 [1] GETGLOBAL 0 -1 ; print
| 2 [1] GETGLOBAL 1 -2 ; tostring
| 3 [1] LOADK 2 -3 ; 0
| 4 [1] CALL 1 2 2
| 5 [1] GETGLOBAL 2 -2 ; tostring
| 6 [1] LOADK 3 -3 ; 0
| 7 [1] CALL 2 2 2
| 8 [1] CONCAT 1 1 2
| 9 [1] CALL 0 2 1
| 10 [2] GETGLOBAL 0 -1 ; print
| 11 [2] GETGLOBAL 1 -2 ; tostring
| 12 [2] LOADK 2 -3 ; 0
| 13 [2] CALL 1 2 2
| 14 [2] GETGLOBAL 2 -2 ; tostring
| 15 [2] LOADK 3 -3 ; 0
| 16 [2] CALL 2 2 2
| 17 [2] CONCAT 1 1 2
| 18 [2] CALL 0 2 1
| 19 [2] RETURN 0 1
Whether this is incorrect depends on whether Lua numbers are
semantically required to have positive and negative zero be distinct
when using doubles underneath. A Lua with a different numeric type
might well have only a single zero anyway.
---> Drake Wilson