lua-users home
lua-l archive

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




On 25 March 2015 at 11:44, Roberto Ierusalimschy <roberto@inf.puc-rio.br> wrote:
> 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.


Read the rest of that paragraph:

  Then, the resulting number is converted to the required type (float or
  integer) according to the previous rules.

Yes, the Arithmetic Operators section describes the behaviour of the reference implementation:

- either the operation is exponentiation or float division and the string is converted to a float
- or, the string is converted to a float

Can I suggest clarifying the "Coercions and Conversions" section of the LRM to reflect this?  As we can see, what I thought was the straight forward statement "Then, the resulting number is converted to the required type (float or integer) according to the previous rules." is ambiguous.  It can be interpreted as either:

- "first" convert(string) -> number:integer; "then" required-type(op, number:integer, number:integer) -> number:integer
- ignore the resulting number created in the first step; required-type(op, string, number:integer) -> float

My weak attempt at re-wording this is:

Strings used in arithmetic operations are converted as follows.  First, the required-type, as specified by the operator's string conversion rules, and described in section Arithmetic Operators, is determined.  Then, the string is converted to the required-type by following the string's syntax and the rules of the lua lexer, further converting to floating-point when needed.




So, you have to know what is "the required type". In the case of addition,
see 3.4.1 – Arithmetic Operators:

  With the exception of exponentiation and float division, 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, the operation is performed following the usual rules for
  floating-point arithmetic (usually the IEEE 754 standard), and the
  result is a float.

That is, "the required type" is float in that case.

-- Roberto