lua-users home
lua-l archive

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


2015-03-25 15:37 GMT+02:00 Andrew Cagney <andrew.cagney@gmail.com>:

> Lua 5.3.0  Copyright (C) 1994-2015 Lua.org, PUC-Rio
>
> First, here are some bits from the manual:
>
> Coercions and Conversions http://www.lua.org/manual/5.3/manual.html#3.4.3
> [...], the string is converted to an integer or a float, following its
> syntax and the rules of the Lua lexer.
>
> Lexical Conventions http://www.lua.org/manual/5.3/manual.html#3.1
> A numeric constant with a fractional dot or an exponent denotes a float;
> otherwise it denotes an integer.
>
> so working backwards:
>
> 0 and 1 both get parsed by the lexer as integers.
>
>> print(0+1)
> 1
>> print(math.type(0+1))
> integer
>
> tonumber() also parses "0" to an integer:
>
>> print(tonumber("0")+1)
> 1
>> print(math.type(tonumber("0")+1))
> integer
>
> yet without the explicit conversion it ends up as a float:
>
>> print("0"+1)
> 1.0
>> print(math.type("0"+1))
> float

There was a full discussion on this at about the rc1 stage, but I am
not a good looker-up of archives. The basic point is that Lua does
not have coerce-to-integer, only coerce-to-number, whatever
default number is (it can be configured to have no floats). However,
tonumber is an explicit conversion, not a coercion, and parsing is
neither.