[R] Behaviour of very large numbers

Duncan Murdoch murdoch at stats.uwo.ca
Thu Aug 30 19:19:23 CEST 2007


On 8/30/2007 12:11 PM, Martin Becker wrote:
> willem vervoort wrote:
>> Dear all,
>> I am struggling to understand this.
>>
>> What happens when you raise a negative value to a power and the result
>> is a very large number?
>>
>>  B
>> [1] 47.73092
>>
>>   
>>> -51^B
>>>     
>> [1] -3.190824e+81
>>
>> # seems fine
>>   
> 
> Well, this seems not to be what you intended to do, you didn't raise a 
> negative value to a power, but you got the negative of a positive number 
> raised to that power (operator precedence, -51^B is the same as -(51^B) 
> and not the same as (-51)^B...).
> 
> If you really want to raise a negative value to a fractional power, you 
> may want to tell R to use complex numbers:
> 
> B <- 47.73092
> x <- complex(real=seq(-51,-49,length=10))
> 
> x^B
> 
>  [1] 2.117003e+81-2.387323e+81i 1.718701e+81-1.938163e+81i
>  [3] 1.394063e+81-1.572071e+81i 1.129702e+81-1.273954e+81i
>  [5] 9.146212e+80-1.031409e+81i 7.397943e+80-8.342587e+80i
>  [7] 5.978186e+80-6.741541e+80i 4.826284e+80-5.442553e+80i
>  [9] 3.892581e+80-4.389625e+80i 3.136461e+80-3.536955e+80i

But watch out if you do this, because of the arbitrary choice of a root. 
  You get oddities like this:

 > x <- complex(real = -1)
 > x
[1] -1+0i
 > 1/x
[1] -1+0i
 > x^(1/3)
[1] 0.5+0.8660254i
 > (1/x)^(1/3)
[1] 0.5-0.8660254i

i.e. even though x and 1/x are equal, the 1/3 powers of them are not.

Duncan Murdoch

P.S. I'm tempted to say, "But don't worry about it, the difference is 
only imaginary", but I'll refrain.



More information about the R-help mailing list