lua-users home
lua-l archive

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


> According to sect 3.4.3 in the refman:
> 
> "The conversion from strings to numbers goes as follows: First, the
> string is converted to an integer or a float, following its syntax
> and the rules of the Lua lexer. (The string may have also leading
> and trailing spaces and a sign.) Then, the resulting number is
> converted to the required type (float or integer) according to the
> previous rules."
> 
> Thus if I have a string which is "42" it should be coerced to an
> integer, since 42 is parsed as an integer constant by the lexer,
> whereas I get:
> 
> print( 43, "42" + 1 ) --> 43   43.0
> 
> where it seems that the addition is a float addition, signaling that
> one of the operands is float. Is this a bug, an error in the refman
> or maybe something on my side (Windows 7-64, using 32bit executables
> compiled with TDM-GCC 4.9.2 32 bit)? Am I missing something?

You are missing the last sentence that you quoted!  "Then, the resulting
number is converted to the required type (float or integer) according
to the previous rules." The "required type" in this case is a float, as
explained in the rule for addition (Section 3.4.1):

  [...] the arithmetic operators work as follows: If both operands are
  integers, the operation is performed over integers and the result is an
  integer. Otherwise, if both operands are numbers or strings that can be
  converted to numbers (see §3.4.3), then they are converted to floats,
  [...]

-- Roberto