gpt4 book ai didi

r - R 的奇怪行为

转载 作者:行者123 更新时间:2023-12-01 16:18:39 25 4
gpt4 key购买 nike

所以,我在 R 中有以下代码:

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

给我输出:

> y  
[1] 0.00000000 0.06024096 0.00000000 0.01098901 0.00000000 0.00000000
[7] 0.01829268 0.00000000 0.06976744 0.00000000 0.04380665 0.01351351
[13] 0.00000000 0.00000000 0.00000000 0.00000000 0.00310559 0.00000000
[19] 0.00000000 0.00000000 0.09957447 0.00000000 0.03738318 0.00000000
> a <- -0.1
> test <- (1/((y+as.numeric(!y))*(a-1)))
> test
[1] -0.9090909 -15.0909091 -0.9090909 -82.7272727 -0.9090909
[6] -0.9090909 -49.6969697 -0.9090909 -13.0303030 -0.9090909
[11] -20.7523511 -67.2727273 -0.9090909 -0.9090909 -0.9090909
[16] -0.9090909 -292.7272727 -0.9090909 -0.9090909 -0.9090909
[21] -9.1297591 -0.9090909 -24.3181818 -0.9090909

> test^a
[1] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
[17] NaN NaN NaN NaN NaN NaN NaN NaN

> -0.9090909^a
[1] -1.009577

我不明白为什么在测试 a 次方时它会给我 NaN,显然,我可以简单地从测试中复制一个值并将其提高到 a 次方,这样就可以了。使用 test[1]^a 也不起作用。

最佳答案

-0.9090909 ^ a-(0.9090909 ^ a) 相同。注意括号。

但是,您的测试 包含负值,您不能对负数求根。尝试 (-0.9090909) ^ a 来验证这一点:

> (-0.9090909) ^ -0.1
[1] NaN

关于r - R 的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27272861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com