[R] Calculating the power of a negative number

Colin Millar C.Millar at MARLAB.AC.UK
Tue Nov 17 17:46:29 CET 2009



If you are trying to solve

x^3 + 2 = 0


I think R will always give the positive root if available

ie

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

So if you wanted all roots you would have to code it yourself... not
sure though


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of Colin Millar
Sent: 17 November 2009 16:10
To: Zhiyuan Jason ZHENG; r-help at stat.math.ethz.ch
Subject: Re: [R] Calculating the power of a negative number

Hi,

Look at 

?NumericConstants


At the bottom of the details section you will find:

"Note that a leading plus or minus is not regarded by the parser as part
of a numeric constant but as a unary operator applied to the constant."


See 

?Syntax 

for precedence information.

Hope this helps,
Colin.


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of Zhiyuan Jason ZHENG
Sent: 17 November 2009 14:00
To: r-help at stat.math.ethz.ch
Subject: [R] Calculating the power of a negative number

Hello,

 

I use R a lot, one thing bugs me is that when I try the following 

> x<- -8

> x^(1/3)

[1] NaN

 

However, it is fine with -8^(1/3). Priority goes to the power. Can you
help
me out for this? Thanks.

 

Best,

 

Zhiyuan J. ZHENG

Ph.D. Candidate

Economic  Department

Virginia Polytechnic Institute and State University

Phone: 540-231-5120 , Blacksburg, VA, 24060

 


	[[alternative HTML version deleted]]

______________________________________________
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.

______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email 

______________________________________________
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.

______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email 
______________________________________________________________________

______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email 




More information about the R-help mailing list