[R] (-8)^(1/3) == NaN?

Thomas Lumley tlumley at u.washington.edu
Mon Jul 20 17:44:00 CEST 2009


On Sun, 19 Jul 2009, jim holtman wrote:

> If the power that a number is being raised to is integer, then is does
> evaluate honoring the unary minus.
>
>> (-2) ^ 5  #integer power
> [1] -32
>> (-2) ^ 5.1
> [1] NaN
>>

Yes. 3 is representable exactly as a whole number, so (-2)^3 exists, but (1/3) is represented as a fraction whose denominator is 2^54, an even number, so (-8)^(1/3) does not exist (as a real number).

More generally, since all floating point numbers are represented as fractions whose denominator is a power of 2, the only way a floating point number can be a legitimate exponent for a negative base is if it represents a whole number.

      -thomas


>
> -8^(1/3)
>
> is parsed as -(8^(1/3)) according to operator precedence.
>
> On Sun, Jul 19, 2009 at 4:49 PM, Liviu Andronic<landronimirc at gmail.com> wrote:
>> On Sun, Jul 19, 2009 at 12:28 AM, jim holtman<jholtman at gmail.com> wrote:
>>> First of all, read FAQ 7.31 to understand that 1/3 is not
>>> representable in floating point.  Also a^b is actually exp(log(a) * b)
>>> and log(-8) is not valid (NaN).
>>>
>>
>> If this is so, why would the following evaluate as expected?
>>> (-8)^(3)
>> [1] -512
>>
>> Liviu
>>
>
>
>
> --
> Jim Holtman
> Cincinnati, OH
> +1 513 646 9390
>
> What is the problem that you are trying to solve?
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

Thomas Lumley			Assoc. Professor, Biostatistics
tlumley at u.washington.edu	University of Washington, Seattle




More information about the R-help mailing list