hi all why does R do this: (-8)^(1/3)=NaN the answer should be : -2 a silly question but i kept on getting errors in some of my code due to this problem. i solve the problem as follows: say we want : (-a)^(1/3) then : sign(a)*(a^(1/3)) works but there has to be a simpler way of soing such a simple mathematical operation. thanking you / allan
> (-8+0i)^(1/3)[1] 1+1.732051i ie complex... On 12/07/05, allan_sta_staff_sci_main_uct at mail.uct.ac.za <allan_sta_staff_sci_main_uct at mail.uct.ac.za> wrote:> hi all > > why does R do this: > > (-8)^(1/3)=NaN > > the answer should be : -2 > > a silly question but i kept on getting errors in some of my code due to this > problem. > > i solve the problem as follows: > > say we want : (-a)^(1/3) > > then : sign(a)*(a^(1/3)) works > > but there has to be a simpler way of soing such a simple mathematical operation. > > thanking you > / > allan > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
Hi I find that one often needs to keep reals real and complexes complex. Try this: "cuberooti" <- function (x) { if (is.complex(x)) { return(sqrt(x + (0+0i))) } sign(x)* abs(x)^(1/3) } best wishes [see that (0+0i) sitting there!] Robin On 12 Jul 2005, at 14:11, allan_sta_staff_sci_main_uct at mail.uct.ac.za wrote:> hi all > > why does R do this: > > (-8)^(1/3)=NaN > > the answer should be : -2 > > a silly question but i kept on getting errors in some of my code > due to this > problem. > > i solve the problem as follows: > > say we want : (-a)^(1/3) > > then : sign(a)*(a^(1/3)) works > > but there has to be a simpler way of soing such a simple > mathematical operation. > > thanking you > / > allan > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting- > guide.html >-- Robin Hankin Uncertainty Analyst National Oceanography Centre, Southampton European Way, Southampton SO14 3ZH, UK tel 023-8059-7743
In general, x^y is evaluated as exp(y*log(x)). In your case, x is negative, so log(x) is NaN. Note also that 1/3 is not represented exactly in your computer anyway, so you would not get an exact cube root this way; e.g.: R> format((1234567891112^3)^(1/3),digits=16) [1] "1234567891112.001" (Probably a bad example, but you get the idea.) In general, sign(x)*(abs(x)^(1/3)) is the way to go for cube roots. David L. Reiner> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch [mailto:r-help- > bounces at stat.math.ethz.ch] On Behalf Of > allan_sta_staff_sci_main_uct at mail.uct.ac.za > Sent: Tuesday, July 12, 2005 8:54 AM > To: Duncan Murdoch > Cc: r-help at stat.math.ethz.ch > Subject: Re: [R] R: to the power > > hi all > > i simply wanted to work with real numbers and thought that (-8)^(1/3) > should > work. > > sorry for not making the question clearer. > > / > allan > > Quoting Duncan Murdoch <murdoch at stats.uwo.ca>: > > > On 7/12/2005 9:29 AM, Robin Hankin wrote: > > > Hi > > > > > > I find that one often needs to keep reals real and complexescomplex.> > > > > > Try this: > > > > > > "cuberooti" <- > > > function (x) > > > { > > > if (is.complex(x)) { > > > return(sqrt(x + (0+0i))) > > > } > > > sign(x)* abs(x)^(1/3) > > > } > > > > > > > > > best wishes > > > > > > [see that (0+0i) sitting there!] > > > > I don't understand this. > > > > 1. I don't think you meant to use sqrt() there, did you?? > > > > 2. What effect does the 0+0i have? x has already been determinedto be> > complex. > > > > Duncan Murdoch > > > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting- > guide.html
At 10:11 12/7/2005, allan_sta_staff_sci_main_uct at mail.uct.ac.za wrote:>hi all > >why does R do this: > >(-8)^(1/3)=NaN > >the answer should be : -2Allan In my computer: > (-8)^(1/3) [1] NaN > -8^(1/3) [1] -2 > -(8^(1/3)) [1] -2 The problem is -8 or the problem is (-8) ? []s Tura -- No virus found in this outgoing message. Checked by AVG Anti-Virus.
On Sat, 16 Jul 2005, Bernardo Rangel Tura wrote:> At 10:11 12/7/2005, allan_sta_staff_sci_main_uct at mail.uct.ac.za wrote: > >> hi all >> >> why does R do this: >> >> (-8)^(1/3)=NaN >> >> the answer should be : -2 >Yes and no. The problem is that the reciprocal of 3 is not exactly representable as a floating point number (it has an infinite binary expansion .010101010101...) So the R expression 1/3 actually returns a number slightly different from one-third. It is a fraction with denominator a power of two (probably 2^53). Now, -8 to power that is a fraction with denominator a power of 2 is not a real number, so, NaN. It would be nice if R could realize that you meant the cube root of -8, but that requires either magical powers or complicated and unreliable heuristics. The real solution might be a function like root(x,a,b) to compute x^(a/b), where a and b could then be exactly representable integers. If someone wants to write one.... -thomas