Well, this is what i got...> -0.0841219200008394^(1/3)[1] -0.438163696867656> (-0.0841219200008394)^(1/3)[1] NaN and i don't have a clue of why this happens or how to avoid it, any suggestions? thank you, Juan
'^' has higher precedence than '-', i.e. your first line is equivalent to - ( 0.08xyz..... ^(1/3) ) Gabor On Sun, Oct 26, 2008 at 9:05 PM, Juan Manuel Barreneche <jumanbar at gmail.com> wrote:> Well, this is what i got... > >> -0.0841219200008394^(1/3) > [1] -0.438163696867656 >> (-0.0841219200008394)^(1/3) > [1] NaN > > and i don't have a clue of why this happens or how to avoid it, any suggestions? > > thank you, > Juan > > ______________________________________________ > 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. >-- Gabor Csardi <Gabor.Csardi at unil.ch> UNIL DGM
On 26/10/2008 4:05 PM, Juan Manuel Barreneche wrote:> Well, this is what i got... > >> -0.0841219200008394^(1/3) > [1] -0.438163696867656 >> (-0.0841219200008394)^(1/3) > [1] NaN > > and i don't have a clue of why this happens or how to avoid it, any suggestions?R won't raise negative numbers to fractional powers, because it uses exp(log(x)*y) for x^y. (If y is a whole number it will work.) Your first case is -(x^y), your second is (-x)^y, so that's why you got the different answers. To avoid it, don't try to take fractional powers of negative numbers. Duncan Murdoch
This comes up from time to time. The problem is that one needs complex numbers to address taking the third root: there are three cube roots for any nonzero number (real or complex). To wit:> > > (-0.0841219200008394+0i)^(1/3) > [1] 0.2190818+0.3794609i > > (-0.0841219200008394-0i)^(1/3) > [1] 0.2190818+0.3794609i > > (-0.0841219200008394+1e-100i)^(1/3) > [1] 0.2190818+0.3794609i > > (-0.0841219200008394-1e-100i)^(1/3) > [1] 0.2190818-0.3794609i > >Note the first two are identical but the second two differ. Anyone care to start discussing signed zero again? [you probably want the *real* cube root, in which case it is best to take minus the unique real cube root of the absolute value:> > -(0.0841219200008394)^(1/3) > [1] -0.4381637 > >(which is what you did, of course!)] HTH rksh Juan Manuel Barreneche wrote:> Well, this is what i got... > > >> -0.0841219200008394^(1/3) >> > [1] -0.438163696867656 > >> (-0.0841219200008394)^(1/3) >> > [1] NaN > > and i don't have a clue of why this happens or how to avoid it, any suggestions? > > thank you, > Juan > > ______________________________________________ > 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. >-- Robin K. S. Hankin Senior Research Associate Cambridge Centre for Climate Change Mitigation Research (4CMR) Department of Land Economy University of Cambridge rksh1 at cam.ac.uk 01223-764877