[R] weird behavior with the 3rd root....

Robin Hankin rksh1 at cam.ac.uk
Mon Oct 27 09:52:45 CET 2008


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



More information about the R-help mailing list