Re: [R] cube root of a negative number
In this particular case it is part of the C99 stanadrd (7.12.7.4) for the 'pow' function R uses. On Wed, 27 Oct 2010, Berwin A Turlach wrote: G'day Gregory, On Tue, 26 Oct 2010 19:05:03 -0400 Gregory Ryslik rsa...@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 1/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 6009e-mail: ber...@maths.uwa.edu.au Australiahttp://www.maths.uwa.edu.au/~berwin __ 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. -- Brian D. Ripley, rip...@stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ 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] cube root of a negative number
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@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] 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 rsa...@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@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] 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 unsubscr...@allianz.com.au with the word unsubscribe in the subject header. __ 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] cube root of a negative number
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.0i 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-boun...@r-project.org [mailto:r-help-boun...@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 rsa...@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@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. __ 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] cube root of a negative number
G'day Gregory, On Tue, 26 Oct 2010 19:05:03 -0400 Gregory Ryslik rsa...@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 1/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 6009e-mail: ber...@maths.uwa.edu.au Australiahttp://www.maths.uwa.edu.au/~berwin __ 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] cube root of a negative number
install.packages('sos')# if you don't have it already library(sos) rs - ???roots # 216 matches summary(rs) # in 106 packages rs # opens a web browser with all 216 matched in a table # listing the package with the most matches first. # This included roots{signal}, which referenced polyroot{base}, # which led me to the following: polyroot(c(4, 0, 0, 1)) [1] 0.793701+1.37473i -1.587401+0.0i 0.793701-1.37473i polyroot(c(4, 0, 1)) [1] 0+2i 0-2i Hope this helps. Spencer # please excuse: I'm the lead author of sos. In my not-so-humble opinion, it's the fastest way to do a literature search for anything statistical. If your search with writeFindFn2lxs does NOT answer your question in a very few minutes, it's OK to look elsewhere. (Please see the vignette for more details if you are not familiar with it.) On 10/26/2010 4:34 PM, bill.venab...@csiro.au wrote: 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.0i 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-boun...@r-project.org [mailto:r-help-boun...@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 Ryslikrsa...@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@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] cube root of a negative number
Because it is implemented as antilog((1/3)*log(-4)) most likely using base 2 for the log/antilog functions. Gregory Ryslik rsa...@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@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 NewmillerThe . . Go Live... DCN:jdnew...@dcn.davis.ca.usBasics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/BatteriesO.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --- Sent from my phone. Please excuse my brevity. __ 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] cube root of a negative number
G'day Bill, On Wed, 27 Oct 2010 10:34:27 +1100 bill.venab...@csiro.au wrote: [...] 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! [...] Mate, you must have been using Excel (or bc) too much recently if this takes you by surprise. :) This is exactly the behaviour that I would expect as I was always taught that exponentiation was the operator with the highest priority. The discussion at http://en.wikipedia.org/wiki/Order_of_operations points out that there exist differing conventions concerning the unary operator - but gives only Excel and bc as examples of programs/languages who give the unary operator the higher precedence. Moreover, http://www.burns-stat.com/pages/Tutor/spreadsheet_addiction.html points out that in Excel the precedence of unary minus is different than many other languages (including VBA which is often used with Excel). Cheers, Berwin __ 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.