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]]
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
Zhiyuan Jason ZHENG wrote:> 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.Well, mycuberoot <- function(x) sign(x)*abs(x)^(1/3) but in general there is just no solution. The problem is that for the solution to be even defined, the power needs to be a fraction with an odd denominator. With floating point arithmetic and roundoff and what not, there is just no way to know whether that is the case or not. In fact, the machine representation of ANY number with a fractional part will be a fraction with an EVEN denominator (2^{p} for some p). -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
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