(-8)^(1/3) == NaN?

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

(-8)^(1/3) == NaN?

Dave DeBarr
Why does the expression "(-8)^(1/3)" return NaN, instead of -2?

This is not answered by http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-are-powers-of-negative-numbers-wrong_003f

Thanks,
Dave


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: (-8)^(1/3) == NaN?

jholtman
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).

You expression is not really taking the cube root; it is taking values
to the 1/3 power.  If you want to cube root function, then try:

> cubeRoot <- function(x) sign(x) * exp(log(abs(x)) / 3)
> cubeRoot(8)
[1] 2
> cubeRoot(-8)
[1] -2
>
>


On Sat, Jul 18, 2009 at 6:04 PM, Dave DeBarr<[hidden email]> wrote:

> Why does the expression "(-8)^(1/3)" return NaN, instead of -2?
>
> This is not answered by http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-are-powers-of-negative-numbers-wrong_003f
>
> Thanks,
> Dave
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.
>



--
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: (-8)^(1/3) == NaN?

David Winsemius
In reply to this post by Dave DeBarr
The correct mathematical "answer" is really one (or perhaps all  
three?) of three complex numbers that are the solutions to x^3+8=0.

Here is one of the others:

 > as.complex(-8)^(1/3)
[1] 1+1.732051i

I suspect there is a reason why R is willing to produce this  
particular solution and not the other two after being told to use the  
complex range, but I don't know the reason.

You can get all three with:
 > polyroot(c(8,0,0,1))
[1]  1+1.732051i -2+0.000000i  1-1.732051i

--
David.

On Jul 18, 2009, at 6:04 PM, Dave DeBarr wrote:

> Why does the expression "(-8)^(1/3)" return NaN, instead of -2?
>
> This is not answered by http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-are-powers-of-negative-numbers-wrong_003f
>
> Thanks,
> Dave

David Winsemius, MD
Heritage Laboratories
West Hartford, CT

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: (-8)^(1/3) == NaN?

Ted.Harding-2
In reply to this post by Dave DeBarr
On 18-Jul-09 22:04:57, Dave DeBarr wrote:
> Why does the expression "(-8)^(1/3)" return NaN, instead of -2?
>
> This is not answered by
> http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-are-powers-of-negative-
> numbers-wrong_003f
>
> Thanks,
> Dave

Because R does not try to evaluate (-8)^(1/3), but (-8)^x, where x is
a very close approximation to 1/3 but is not exactly 1/3 (which is
impossible in a finite binary representation).

But even if it could exactly represent 1/3, R would still need to
have a special "look-up" for certain fractional powers (1/3, 1/5, ... )
to enable it to recognise that these are odd-integer-roots of negatgive
numbers, and therefore can be evaulated as -(nth_root(abs(x))).

It doesn't help, either, to try to do it in complex numbers, since
(-8) will then be seen as 8*exp(i*pi) whose cube root will be found as

2*exp(i*pi/3) = 2*(cos(pi/3) + i*sin(pi/3)) = 2*(1/2 + i*sqrt(3)/2):

  (complex(1,-8,0))
  # [1] -8+0i

  complex(1,-8,0)^(1/3)
  # [1] 1+1.732051i

  (8*exp(complex(1,0,pi)))^(1/3)
  # [1] 1+1.732051i

  sqrt(3)
  # [1] 1.732051

I'm not sure what best to suggest for your situation. Basically, if it
is in a context where it can only be

  (negative number)^(1/(odd integer))

then you are better off modifying the logic of your program so as
to ensure the result you want.

Hoping this helps,
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <[hidden email]>
Fax-to-email: +44 (0)870 094 0861
Date: 18-Jul-09                                       Time: 23:54:11
------------------------------ XFMail ------------------------------

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: (-8)^(1/3) == NaN?

Victor Manuel Garcia Guerrero
In reply to this post by Dave DeBarr
It' true, but if you type -8^(1/3) returns -2, and if you type -8^1/3 it returns -2.66666, maybe there are some rules about parenthesis...

regards

Víctor


________________________________

De: [hidden email] en nombre de Dave DeBarr
Enviado el: sáb 18/07/2009 05:04
Para: [hidden email]
Asunto: [R] (-8)^(1/3) == NaN?



Why does the expression "(-8)^(1/3)" return NaN, instead of -2?

This is not answered by http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-are-powers-of-negative-numbers-wrong_003f

Thanks,
Dave


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: (-8)^(1/3) == NaN?

jholtman
It is the order of operator precedence.  Look at what you typed:

-8^1/3 is parsed as -(8^1)/3 = -2.66666

On Sun, Jul 19, 2009 at 1:12 PM, Victor Manuel Garcia
Guerrero<[hidden email]> wrote:

> It' true, but if you type -8^(1/3) returns -2, and if you type -8^1/3 it returns -2.66666, maybe there are some rules about parenthesis...
>
> regards
>
> Víctor
>
>
> ________________________________
>
> De: [hidden email] en nombre de Dave DeBarr
> Enviado el: sáb 18/07/2009 05:04
> Para: [hidden email]
> Asunto: [R] (-8)^(1/3) == NaN?
>
>
>
> Why does the expression "(-8)^(1/3)" return NaN, instead of -2?
>
> This is not answered by http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-are-powers-of-negative-numbers-wrong_003f
>
> Thanks,
> Dave
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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.
>



--
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: (-8)^(1/3) == NaN?

Liviu Andronic
On Sun, Jul 19, 2009 at 7:33 PM, jim holtman<[hidden email]> wrote:
> -8^1/3 is parsed as -(8^1)/3 = -2.66666
>

However the following is evaluated as one would expect:
> 8^(1/3)
[1] 2
> -8^(1/3)
[1] -2

Perhaps it is parsed in this way:
> -(8^(1/3))
[1] -2

Liviu

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: (-8)^(1/3) == NaN?

Liviu Andronic
In reply to this post by jholtman
On Sun, Jul 19, 2009 at 12:28 AM, jim holtman<[hidden email]> 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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: (-8)^(1/3) == NaN?

jholtman
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
>


-8^(1/3)

is parsed as -(8^(1/3)) according to operator precedence.

On Sun, Jul 19, 2009 at 4:49 PM, Liviu Andronic<[hidden email]> wrote:

> On Sun, Jul 19, 2009 at 12:28 AM, jim holtman<[hidden email]> 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?

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: (-8)^(1/3) == NaN?

Rolf Turner

On 20/07/2009, at 9:13 AM, 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

        <snip>

I was vaguely aware of this ... but it now triggers in my mind the
question of how the ^ function decides when the exponent is an integer.

A bit of experimentation seems to indicate that, e.g., (-2)^x ``works''
if (and only if?) round(x)==x returns TRUE.

Note that (-2)^x may NOT ``work'' in some cases were all.equal(x,round
(x))
returns TRUE.

Young players should also be aware of the following trap.  It
can happen that n + epsilon ``is an integer'' according to my
rule, but m + epsilon is NOT an integer according to this rule.
Where m and n are both integers.

E.g.:

 > eps <- 0.4e-15
 > x <- 5+eps
 > x==round(x)
[1] TRUE
 > y <- 3+eps
 > y==round(y)
[1] FALSE

This is of course due to the exigencies of how n and m are represented
in floating point arithmetic.  Not too deep once you're aware of the
problem, but it can still be a ``gotcha'' if one is not alert.

(Be alert.  The world needs more lerts!)

        cheers,

                Rolf Turner

P. S.  Perhaps young players should be reminded at this point that  
is.integer() is
no help here.  This function tells you about the ***storage mode***  
of its argument.
Only.

                R. T.


######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: (-8)^(1/3) == NaN?

jholtman
It also works for raising a number to a negative integer:

> (-3)^(-3)
[1] -0.03703704
>


On Sun, Jul 19, 2009 at 6:23 PM, Rolf Turner<[hidden email]> wrote:

>
> On 20/07/2009, at 9:13 AM, 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
>
>        <snip>
>
> I was vaguely aware of this ... but it now triggers in my mind the
> question of how the ^ function decides when the exponent is an integer.
>
> A bit of experimentation seems to indicate that, e.g., (-2)^x ``works''
> if (and only if?) round(x)==x returns TRUE.
>
> Note that (-2)^x may NOT ``work'' in some cases were all.equal(x,round(x))
> returns TRUE.
>
> Young players should also be aware of the following trap.  It
> can happen that n + epsilon ``is an integer'' according to my
> rule, but m + epsilon is NOT an integer according to this rule.
> Where m and n are both integers.
>
> E.g.:
>
>> eps <- 0.4e-15
>> x <- 5+eps
>> x==round(x)
> [1] TRUE
>> y <- 3+eps
>> y==round(y)
> [1] FALSE
>
> This is of course due to the exigencies of how n and m are represented
> in floating point arithmetic.  Not too deep once you're aware of the
> problem, but it can still be a ``gotcha'' if one is not alert.
>
> (Be alert.  The world needs more lerts!)
>
>        cheers,
>
>                Rolf Turner
>
> P. S.  Perhaps young players should be reminded at this point that
> is.integer() is
> no help here.  This function tells you about the ***storage mode*** of its
> argument.
> Only.
>
>                R. T.
>
>
> ######################################################################
> Attention:This e-mail message is privileged and confidential. If you are not
> theintended recipient please delete the message and notify the sender.Any
> views or opinions presented are solely those of the author.
>
> This e-mail has been scanned and cleared by
> MailMarshalwww.marshalsoftware.com
> ######################################################################
>



--
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: (-8)^(1/3) == NaN?

Thomas Lumley
In reply to this post by jholtman
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<[hidden email]> wrote:
>> On Sun, Jul 19, 2009 at 12:28 AM, jim holtman<[hidden email]> 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?
>
> ______________________________________________
> [hidden email] 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
[hidden email] University of Washington, Seattle

______________________________________________
[hidden email] 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.