lua-users home
lua-l archive

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


On Tue, May 21, 2019 at 4:01 PM Christian Thaeter <ct@pipapo.org> wrote:
>
> On 2019-05-21 22:29, Egor Skriptunoff wrote:
>
> > 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)
>
> Program bytes or bytecode?
>
> I throw
>
> x=x-x%1
>
> into the game, but I only tested it on lua5.2, ignore me when it doest
> work on other versions, also missed your goal of 13 bytes.
>
>         Christian

In Lua 5.3, that fails to convert x to an integer. It remains a float.