Hi, This might be me missing something painfully obvious but why does the cube root of the following produce an NaN?> (-4)^(1/3)[1] NaN>As we can see:> (-1.587401)^3[1] -4 Thanks! Greg
Look at this:> x <- as.complex(-4) > x[1] -4+0i> x^(1/3)[1] 0.793701+1.37473i> (-4)^(1/3)[1] NaN It seems that R gives you the principal root, which is complex, and not the real root. Kjetil On Tue, Oct 26, 2010 at 8:05 PM, Gregory Ryslik <rsaber at comcast.net> wrote:> Hi, > > This might be me missing something painfully obvious but why does the cube root of the following produce an NaN? > >> (-4)^(1/3) > [1] NaN >> > > As we can see: > >> (-1.587401)^3 > [1] -4 > > Thanks! > > Greg > ______________________________________________ > 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. >
sachinthaka.abeywardana at allianz.com.au
2010-Oct-26 23:28 UTC
[R] cube root of a negative number
hmm interesting. When I did -4^(1/3) got the correct answer, but then again that's because it processes the negative later. i.e. -4^(1/2) gave me -2 instead of the 2i I expected. Also when I did (-4+0i)^(1/3) it gave me 0.793701+1.37473i. Possible bug? Sachin --- Please consider the environment before printing this email --- Allianz - Best General Insurance Company of the Year 2010* Allianz - General Insurance Company of the Year 2009+ * Australian Banking and Finance Insurance Awards + Australia and New Zealand Insurance Industry Awards This email and any attachments has been sent by Allianz Australia Insurance Limited (ABN 15 000 122 850) and is intended solely for the addressee. It is confidential, may contain personal information and may be subject to legal professional privilege. Unauthorised use is strictly prohibited and may be unlawful. If you have received this by mistake, confidentiality and any legal privilege are not waived or lost and we ask that you contact the sender and delete and destroy this and any other copies. In relation to any legal use you may make of the contents of this email, you must ensure that you comply with the Privacy Act (Cth) 1988 and you should note that the contents may be subject to copyright and therefore may not be reproduced, communicated or adapted without the express consent of the owner of the copyright. Allianz will not be liable in connection with any data corruption, interruption, delay, computer virus or unauthorised access or amendment to the contents of this email. If this email is a commercial electronic message and you would prefer not to receive further commercial electronic messages from Allianz, please forward a copy of this email to unsubscribe at allianz.com.au with the word unsubscribe in the subject header.
To take it one step further:> x <- as.complex(-4) > cx <- x^(1/3) > > r <- complex(modulus = Mod(cx), argument = Arg(cx)*c(1,3,5)) > r[1] 0.793701+1.37473i -1.587401+0.00000i 0.793701-1.37473i> r^3[1] -4+0i -4+0i -4+0i>So when you ask for "the" cube root of -4, R has a choice of three possible answers it can give you. It is no surprise that this does not work when working in the real domain, except "by fluke" with something like> -4^(1/3)[1] -1.587401>where the precedence of the operators is not what you might expect. Now that could be considered a bug, since apparently> -4^(1/2)[1] -2 which comes as rather a surprise! Bill. -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Kjetil Halvorsen Sent: Wednesday, 27 October 2010 9:17 AM To: Gregory Ryslik Cc: r-help Help Subject: Re: [R] cube root of a negative number Look at this:> x <- as.complex(-4) > x[1] -4+0i> x^(1/3)[1] 0.793701+1.37473i> (-4)^(1/3)[1] NaN It seems that R gives you the principal root, which is complex, and not the real root. Kjetil On Tue, Oct 26, 2010 at 8:05 PM, Gregory Ryslik <rsaber at comcast.net> wrote:> Hi, > > This might be me missing something painfully obvious but why does the cube root of the following produce an NaN? > >> (-4)^(1/3) > [1] NaN >> > > As we can see: > >> (-1.587401)^3 > [1] -4 > > Thanks! > > Greg > ______________________________________________ > 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. >______________________________________________ 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.
G'day Gregory, On Tue, 26 Oct 2010 19:05:03 -0400 Gregory Ryslik <rsaber at comcast.net> wrote:> Hi, > > This might be me missing something painfully obvious but why does the > cube root of the following produce an NaN? > > > (-4)^(1/3) > [1] NaN1/3 is not exactly representable as a binary number. My guess is that the number that is closest to 1/3 and representable cannot be used as the exponent for negative numbers, hence the NaN. Essentially, don't expect finite precision arithmetic to behave like infinite precision arithmetic, it just doesn't. The resources mentioned in FAQ 7.31 can probably shed more light on this issue. Cheers, Berwin ========================== Full address ===========================Berwin A Turlach Tel.: +61 (8) 6488 3338 (secr) School of Maths and Stats (M019) +61 (8) 6488 3383 (self) The University of Western Australia FAX : +61 (8) 6488 1028 35 Stirling Highway Crawley WA 6009 e-mail: berwin at maths.uwa.edu.au Australia http://www.maths.uwa.edu.au/~berwin
Because it is implemented as antilog((1/3)*log(-4)) most likely using base 2 for the log/antilog functions. "Gregory Ryslik" <rsaber at comcast.net> wrote:>Hi, > >This might be me missing something painfully obvious but why does the >cube root of the following produce an NaN? > >> (-4)^(1/3) >[1] NaN >> > >As we can see: > >> (-1.587401)^3 >[1] -4 > >Thanks! > >Greg >______________________________________________ >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.--------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity.
Seemingly Similar Threads
- cube root
- approxfun-problems (yleft and yright ignored)
- [PATCH] Always use mipmaps in cube plugin
- workspaces/desktops/viewports on top/bottom of cube?
- [PATCH] add 'sticky' mode to cube to allow sticky+not-on-bottom windows to stick to the screen when the screen is transformed