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

2009-07-20 Thread Thomas Lumley

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 Androniclandronim...@gmail.com wrote:

On Sun, Jul 19, 2009 at 12:28 AM, jim holtmanjholt...@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@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
tlum...@u.washington.eduUniversity of Washington, Seattle

__
R-help@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.


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

2009-07-19 Thread Victor Manuel Garcia Guerrero
It' true, but if you type -8^(1/3) returns -2, and if you type -8^1/3 it 
returns -2.6, maybe there are some rules about parenthesis...

regards

Víctor 




De: r-help-boun...@r-project.org en nombre de Dave DeBarr
Enviado el: sáb 18/07/2009 05:04
Para: r-help@r-project.org
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]]

__
R-help@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.

__
R-help@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.


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

2009-07-19 Thread jim holtman
It is the order of operator precedence.  Look at what you typed:

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

On Sun, Jul 19, 2009 at 1:12 PM, Victor Manuel Garcia
Guerrerovmgar...@colmex.mx wrote:
 It' true, but if you type -8^(1/3) returns -2, and if you type -8^1/3 it 
 returns -2.6, maybe there are some rules about parenthesis...

 regards

 Víctor


 

 De: r-help-boun...@r-project.org en nombre de Dave DeBarr
 Enviado el: sáb 18/07/2009 05:04
 Para: r-help@r-project.org
 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]]

 __
 R-help@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.

 __
 R-help@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.




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

What is the problem that you are trying to solve?

__
R-help@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.


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

2009-07-19 Thread Liviu Andronic
On Sun, Jul 19, 2009 at 7:33 PM, jim holtmanjholt...@gmail.com wrote:
 -8^1/3 is parsed as -(8^1)/3 = -2.6


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

__
R-help@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.


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

2009-07-19 Thread Liviu Andronic
On Sun, Jul 19, 2009 at 12:28 AM, jim holtmanjholt...@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

__
R-help@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.


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

2009-07-19 Thread jim holtman
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 Androniclandronim...@gmail.com wrote:
 On Sun, Jul 19, 2009 at 12:28 AM, jim holtmanjholt...@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@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.


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

2009-07-19 Thread 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}}

__
R-help@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.


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

2009-07-19 Thread jim holtman
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 Turnerr.tur...@auckland.ac.nz 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?

__
R-help@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.


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

2009-07-18 Thread 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]]

__
R-help@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.


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

2009-07-18 Thread jim holtman
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 DeBarrdave.deb...@microsoft.com 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]]

 __
 R-help@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.




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

What is the problem that you are trying to solve?

__
R-help@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.


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

2009-07-18 Thread David Winsemius
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.00i  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

__
R-help@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.


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

2009-07-18 Thread Ted Harding
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) ted.hard...@manchester.ac.uk
Fax-to-email: +44 (0)870 094 0861
Date: 18-Jul-09   Time: 23:54:11
-- XFMail --

__
R-help@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.