Hi [email protected] napsal dne 16.11.2009 13:27:03:
> but with complex, I get complex numbers for the first and last elements: > > > (as.complex(tmp))^(1/3) > [1] 0.01969170+0.03410703i 0.03478442+0.00000000i 0.03285672+0.00000000i > [4] 0.08950802+0.00000000i 0.05848363+0.10129661i And that is a right answer > > whereas for the first element, we get the followings. > > Moreover, > > -6.108576e-05^(1/3) > > [1] -0.03938341 > and > > -(6.108576e-05^(1/3)) > [1] -0.03938341 > and > -((6.108576e-05)^(1/3)) > [1] -0.03938341 > No. With all constructions like above you compute a cube root of ***positive*** number and then you put *-* sign before the result, hence the same result. Try instead to make a cube root of negative number. (-6.108576e-05)^(1/3) [1] NaN This is what you exactly do by the first call. Beware also that 1/3 is not exactly representable in binary arithmetic and so you actually do not compute cube root but some root which is quite near to cube root. > (1000^(1/3))-10 [1] -1.776357e-15 If you want cube root and have negative numbers you need probably something like sign(tmp) * abs(tmp)^(1/3) > give the same results. > > so using () doesn't preserve any thing You need to use parentheses on correct places. To see what is the precedence of operators see ?Syntax Regards Petr > > --- On Mon, 11/16/09, Petr PIKAL <[email protected]> wrote: > > > From: Petr PIKAL <[email protected]> > > Subject: Odp: [R] ^ operator > > To: "carol white" <[email protected]> > > Cc: [email protected] > > Date: Monday, November 16, 2009, 3:40 AM > > Hi > > > > AFAIK, this is issue of the preference of operators. > > > > [email protected] > > napsal dne 16.11.2009 11:24:59: > > > > > Hi, > > > I want to apply ^ operator to a vector but it is > > applied to some of the > > > elements correctly and to some others, it generates > > NaN. Why is it not > > able to > > > calculate -6.108576e-05^(1/3) even though it exists? > > > > > > > > > tmp > > > [1] -6.108576e-05 4.208762e-05 > > 3.547092e-05 7.171101e-04 > > -1.600269e-03 > > > > tmp^(1/3) > > > [1] NaN 0.03478442 > > 0.03285672 0.08950802 NaN > > > > This computes (-a)^(1/3) which is not possible in real > > numbers. You have > > to use as.complex(tmp)^(1/3) to get a result. > > > > > > -6.108576e-05^(1/3) > > > [1] -0.03938341 > > > > this is actually > > -(6.108576e-05^(1/3)) > > > > Regards > > Petr > > > > > > > > > > ______________________________________________ > > > [email protected] > > 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. > > > > > > > > > ______________________________________________ > [email protected] 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. ______________________________________________ [email protected] 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.

