lua-users home
lua-l archive

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


On Sun, Apr 21, 2013 at 6:45 PM, KHMan <keinhong@gmail.com> wrote:
> On 4/22/2013 4:26 AM, [ex] wrote:
>>
>> I'm using Lua 5.2 as the foundation of my own language (Killa).
>> Killa has a JavaScript like syntax and hence supports:
>>
>>       alert(9.9 | 0); // Alerts 9. (x|0) is used a lot in JS to
>> floor x.
>>
>> the code in Lua:
>>
>>       print(bit32.bor(9.9, 0)) -- prints 10, not 9
>
>
> JavaScript should have fixed this kind of defect, they had ample releases to
> do so. Non-integers for a bitwise function? This is like texting while
> driving... :-)
>
> Just use math.floor in Lua.
>
> --
> Cheers,
> Kein-Hong Man (esq.)
> Kuala Lumpur, Malaysia
>
>

This *is* the way JS fixed it. Bitwise operations coerce to integer,
and x|0 is a lot faster than Math.floor(x), plus also the bitop gives
JIT optimizers a very powerful hint that the value is an integer and
will probably always be one.

/s/ Adam