lua-users home
lua-l archive

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


Excerpts from Liam Devine's message of 2014-11-16 13:09:38 UTC:
> What is the current recommended method of converting a float to int?
> Should it be to check if it is greater than zero and floor else use
> ceiling and then finally use one of the above conversion methods?
> 
> If this behaviour is to stay, would it be time to have a cast operator
> int() ?

I'm not particularly fond of Lua 5.3 overzealous checking of fractions
and ranges when doing float->int integers conversions either.

One quick hack to perform "just convert float to int no matter what, and
to the hell with the consequences, like C would do" is:

int = string.format("%.0f", float))|0

I wish there was some built-in support for this (say, math.to_unsafe_int() or
something).