Strange behavior of R

前端 未结 2 1823
感情败类
感情败类 2021-01-21 15:52

So, I have the following code in R:

y
a <- -0.1    
test <- (1/((y+as.numeric(!y))*(a-1)))  
test  
test^a  
-0.9090909^a 

Giving me the

相关标签:
2条回答
  • 2021-01-21 16:16

    You are trying to take a root of a negative number. If you wish to do complex arithmetic you need to convert to complex

    as.complex(test) ^ a
    # [1] 0.9601644-0.3119763i 0.7249946-0.2355650i 0.9601644-0.3119763i
    # [4] 0.6115633-0.1987090i 0.9601644-0.3119763i 0.9601644-0.3119763i
    # [7] 0.6435367-0.2090978i 0.9601644-0.3119763i 0.7357171-0.2390490i
    #[10] 0.9601644-0.3119763i 0.7022627-0.2281790i 0.6243418-0.2028609i
    #[13] 0.9601644-0.3119763i 0.9601644-0.3119763i 0.9601644-0.3119763i
    #[16] 0.9601644-0.3119763i 0.5389643-0.1751201i 0.9601644-0.3119763i
    #[19] 0.9601644-0.3119763i 0.9601644-0.3119763i 0.7623605-0.2477059i
    #[22] 0.9601644-0.3119763i 0.6912151-0.2245894i 0.9601644-0.3119763i
    
    0 讨论(0)
  • 2021-01-21 16:21

    -0.9090909 ^ a is the same as -(0.9090909 ^ a). Notice the parentheses.

    However, your test contains negative values, and you cannot take the root of a negative number. Try (-0.9090909) ^ a to verify this:

    > (-0.9090909) ^ -0.1
    [1] NaN
    
    0 讨论(0)
提交回复
热议问题