• Subject: Re: compute cubic root with negative argument
• From: Dirk Laurie <dirk.laurie@...>
• Date: Thu, 14 Apr 2016 13:36:04 +0200

```2016-04-14 13:17 GMT+02:00 Dirk Laurie <dirk.laurie@gmail.com>:
> 2016-04-14 12:30 GMT+02:00 Christopher Kappe <kappe@cs.uni-kl.de>:
>> I just noticed that it is not possible to compute the cubic root of a
>> negative number. See e.g.
>> print( math.pow( -27, 1/3 ) ) --> -nan instead of -3
>> The same is true for the ^ operator (not surprising).
>>
>> This probably stems from the fact that the result of the n-th root of a
>> negative number is not real if n is even
>
> No, it stems from the fact that math.pow cannot know that 1/3 is
> a rational number. It cannot even know that 0.5 is a rational number
> and not the floating-point approximation to an irrational number.
>
> The best solution to this is to write a

[Sorry, it seems to be possible to send an e-mail by hitting the
wrong key.]

littte routine rpow(x,p,q) for x^(p/q) that will take the parity of q into
account. It may also take into account that there are cases (like
rpow(-27,1,3) when the result should be an integer.

>> The least one should do, is document the fact that exponentiation
>> only works for a non-negative base argument.

The Lua documentation already says "Exponentiation uses the
ISO C function pow". That is clean an unambiguous.

"man pow" says inter alia:

If x is a finite value less than 0, and y is  a  finite  noninteger,  a
domain error occurs, and a NaN is returned.

If the Lua manual is expected to repeat that, why must it not also
repeat the 20 other cases documented by "man pow" of what
happens when the user provides input for which the returned value
is not simply "the value of x to the power of y"?

```