lua-users home
lua-l archive

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


On Apr 13, 2014, at 1:34 PM, Roberto Ierusalimschy <roberto@inf.puc-rio.br> wrote:

>> (I already had to change existing Lua code to get it running with
>> work2.) Wouldn't it be more logical, if exponentiation with negative
>> exponent would convert the base argument to float, similar to the
>> division case?
> 
> Lua 5.3 work 1 worked that way. We changed to follow a guideline that
> the type of the result of an operation should not depend on the *values*
> of the operands, only on their types. (Note that all other operators
> satisfy this rule; the division, in particular, always convert its
> arguments to floats.)
> 
> As others pointed out already, integer exponentiation with full 64 bits
> is useful (not to mention that it is usually faster, too). To make 2^3
> an integer and 2^-3 a float breaks that guideline. But maybe that case
> is worth the break.
> 
> -- Roberto
> 

If you assume the usual mathematical identity: x^-n == 1 / (x^n), is there any reason why 2^-3 cannot be treated as 1 // (2^3) ? This keeps the “type only” rule for how the operator is interpreted (which i agree with 100%). it also avoids surprises since, like other binary operators, if both values are integers you get an integer result (with the exception of division).

—Tim