[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: integer exponentiation with negative exponent
- From: Dirk Laurie <dirk.laurie@...>
- Date: Sat, 12 Apr 2014 10:34:13 +0200
2014-04-12 0:57 GMT+02:00 Hartmut Henkel <hartmut_henkel@gmx.de>:
> Hi,
>
> this works (Lua 5.3.0 (work2)):
>> =1 / 4
> 0.25
>
> but the same calculation just written differently gives an error:
>> =4^-1
> stdin:1: integer exponentiation with negative exponent
>
> This error case looks counter-intuitive to me. E. g. calc is happy:
>
> ; 4^-1
> 0.25
>
> same with awk:
>
> $ awk 'BEGIN{print(4^-1)}'
> 0.25
>
> (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?
>
> Regards, Hartmut
>
The manual states:
| With the exception of division and integer 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.
IMHO exponentiation is also special and should be defined as follows:
1. If the exponent can be exactly represented by a positive integer,
the result is evaluated by an algorithm mathematically equivalent to
repeated multiplication by the first operand. When that is an integer,
the result is exact when there is no overflow, but may be apparently
meaningless otherwise.
2. In all other cases, the argument is converted to floating-point
before doing the exponentiation.