我遇到了R中asin()
函数的奇怪行为。我有一个偶尔计算为-1的表达式,我传递给asin()
。有时asin()
以其他时间以NaNs produced
响应预期值asin(-1) = 1.57....
。asin()中的意外行为
下面的代码给出的例子(从更大的函数,计算生长度日昆虫):
# works
tempMax <- 22.6
tempMin <- 10.0
threshold <- 10
meanT <- (tempMax + tempMin)/2
amplitude <- (tempMax - tempMin) /2
thetaSub <- ((threshold - meanT)/amplitude)
thetaOut <- asin(thetaSub)
# fails
tempMax <- 22.7
tempMin <- 10.0
threshold <- 10
meanT <- (tempMax + tempMin)/2
amplitude <- (tempMax - tempMin) /2
thetaSub <- ((threshold - meanT)/amplitude)
thetaOut <- asin(thetaSub)
注意,在这两个例子thetaSub
的计算结果为-1,但只asin
“作品”在第一个例子。
当tempMin == threshold
和tempMax
的整数值为偶数且小数部分为.7(例如22.7,24.7,26.7)时,测试函数似乎得到了NaN。
我怀疑这不是原因,但只是其他情况引发相同的错误。我猜这与thetaSub
的价值如何被asin
解释有关,但我无法弄清楚为什么它有时会起作用,而不是其他的。
编辑。
@詹姆斯已经确定我的问题是浮点问题。我如何强制asin'忽略'小数位?
第二个'thetaSub'实际上低于'-1'。 –
回复:您的编辑,我扩大了我的答案与可能的解决方案 – James