lua-users home
lua-l archive

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


This post is just for fun.

Global variable "x" contains some integer number in float format.
Your task is to convert it to integer datatype.

The code must be compatible with Lua 5.1, 5.2 and 5.3:
1) on Lua 5.1/5.2 variable x doesn't change its value;
2) on Lua 5.3 variable x converts its float value to corresponding integer value.


This is not a solution (due to syntax error is raised on Lua 5.1):
x=x|0

Obvious solution takes 15 bytes:
x=math.floor(x)

Less obvious solution takes 14 bytes:
x=math.ceil(x)

There exist non-obvious solution in 13 bytes.
Find it.