[NoW] Small float constants are silently replaced with integers

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[NoW] Small float constants are silently replaced with integers

Egor Skriptunoff-2
Hi!
  
-- Lua 5.4 from GitHub
local x = setmetatable({}, {
   __add      = print,
   __bor      = print,
   __shl      = print,
   __lt       = print,
   __index    = print,
   __newindex = print
})
local y
y = x + 42       -->   table: 00000000005FADA0   42
y = x + 42.0     -->   table: 00000000005FADA0   42.0
y = x | 42       -->   table: 00000000005FADA0   42
y = x | 42.0     -->   table: 00000000005FADA0   42.0
y = x << 42      -->   table: 00000000005FADA0   42
y = x << 42.0    -->   table: 00000000005FADA0   42.0
y = x < 42       -->   table: 00000000005FADA0   42
y = x < 42.0     -->   table: 00000000005FADA0   42
y = x[42]        -->   table: 00000000005FADA0   42
y = x[42.0]      -->   table: 00000000005FADA0   42.0
x[42]   = y      -->   table: 00000000005FADA0   42     nil
x[42.0] = y      -->   table: 00000000005FADA0   42.0   nil
 
Why comparison operator ignores numeric subtype of a small float number?
It is risky:
while calculating result of "x < 42.0" inside a metamethod,
an unexpected integer overflow might occur in arithmetic operation
due to constant 42.0 (provided by user) was silently replaced with 42
 
More consistency is required here.
IMO, numeric subtype of arguments should always be preserved.

Reply | Threaded
Open this post in threaded view
|

Re: [NoW] Small float constants are silently replaced with integers

Dirk Laurie-2
Op Wo. 20 Mrt. 2019 om 22:57 het Egor Skriptunoff
<[hidden email]> geskryf:

> -- Lua 5.4 from GitHub
> local x = setmetatable({}, {
>    __add      = print,
>    __bor      = print,
>    __shl      = print,
>    __lt       = print,
>    __index    = print,
>    __newindex = print
> })
> local y
> y = x + 42       -->   table: 00000000005FADA0   42
> y = x + 42.0     -->   table: 00000000005FADA0   42.0
> y = x | 42       -->   table: 00000000005FADA0   42
> y = x | 42.0     -->   table: 00000000005FADA0   42.0
> y = x << 42      -->   table: 00000000005FADA0   42
> y = x << 42.0    -->   table: 00000000005FADA0   42.0
> y = x < 42       -->   table: 00000000005FADA0   42
> y = x < 42.0     -->   table: 00000000005FADA0   42
> y = x[42]        -->   table: 00000000005FADA0   42
> y = x[42.0]      -->   table: 00000000005FADA0   42.0
> x[42]   = y      -->   table: 00000000005FADA0   42     nil
> x[42.0] = y      -->   table: 00000000005FADA0   42.0   nil
>
> Why comparison operator ignores numeric subtype of a small float number?
> It is risky:
> while calculating result of "x < 42.0" inside a metamethod,
> an unexpected integer overflow might occur in arithmetic operation
> due to constant 42.0 (provided by user) was silently replaced with 42
>
> More consistency is required here.
> IMO, numeric subtype of arguments should always be preserved.

I have verified that Lua 5.3.5 does not do this. Good spot.

Reply | Threaded
Open this post in threaded view
|

Re: [NoW] Small float constants are silently replaced with integers

Luiz Henrique de Figueiredo
> I have verified that Lua 5.3.5 does not do this. Good spot.

The VM in Lua 5.4 introduced OP_LTI which has immediate operands.