[R] Is it a bug ?

Thomas Lumley tlumley at u.washington.edu
Thu Jul 5 16:48:39 CEST 2007


On Thu, 5 Jul 2007, Giuseppe PEDRAZZI wrote:
> I am using R 2.5.0,  windows XP - italian language.
>
> I was perfoming some calculation on fractional exponential and
> I found a strange behaviour. I do not know if it is really a bug, but I would expect
> a different answer from R.
>
> I was trying the following :
>
> x <- seq(-3,3, by =0.1)
> n <- 2.2
> y <- exp(-x^n)
>
> well, the y vector contains (NaN for all negative value of x)

Yes. Non-integer powers of negative numbers are undefined (unless you use 
complex numbers).

> but if you ask for single value calculation like
>
> y <- exp(-(-3)^2.2) or
>
> y <- exp(-(-2.9)^2.2)
>
> the answer is correct.

I get NaN for both of these.  Perhaps you mean exp(-2.9^2.2)? This gives 
a valid answer, but that is because it is exp(-(2.9^2.2)) not 
exp((-2.9)^2.2)

> It seem it does not make the calculation in vector form.
>
> I got the same behaviour (NaN)  in a for loop
>
>> for(i in 1:length(x)) y[i]=exp(x[i]^n)
>> y
> [1]           NaN          NaN          NaN          NaN          NaN          NaN          NaN          NaN          NaN
> [10]          NaN          NaN          NaN          NaN          NaN          NaN          NaN          NaN          NaN
> [19]          NaN          NaN          NaN          NaN          NaN          NaN          NaN          NaN          NaN
> [28]          NaN          NaN          NaN     1.000000     1.006330     1.029416     1.073302     1.142488     1.243137
> [37]     1.384082     1.578166     1.844237     2.210260     2.718282     3.432491     4.452553     5.936068     8.137120
> [46]    11.473746    16.648415    24.867680    38.251295    60.611092    98.967689   166.572985   289.077778   517.425935
> [55]   955.487320  1820.793570  3581.521323  7273.674928 15255.446778 33050.861013 73982.100407
>>
>
> Is it strange or did I miss something ?

You missed something. It is not clear what you missed because some of your 
examples do not give the answer you say they give.

 	-thomas



More information about the R-help mailing list