[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: [NoW] Small float constants are silently replaced with integers
- From: Dirk Laurie <dirk.laurie@...>
- Date: Thu, 21 Mar 2019 10:13:32 +0200
Op Wo. 20 Mrt. 2019 om 22:57 het Egor Skriptunoff
<egor.skriptunoff@gmail.com> 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.