lua-users home
lua-l archive

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


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.