==在R中,具有.Machine $ double.eps精度 [英] == in R, with .Machine$double.eps accuracy
本文介绍了==在R中,具有.Machine $ double.eps精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在R中,我不得不转换诸如以下的易读代码有点烦人:
In R, I find it a bit annoying to have to transform easy-to-read code like:
if (det(A) == 1) # not always working because of floating point precision
...
到
if (abs(det(A) - 1) < .Machine$double.eps) # working but bad for readability
...
问题:R中是否有内置运算符可以测试值是否等于".Machine$double.eps
错误"?像这样的东西:
Question: is there a built-in operator in R that tests if values are equal "up to a .Machine$double.eps
error"? Something like:
if (det(A) ==~ 1) # TRUE even if det(A) = 1 + 1e-17
...
推荐答案
一种方法是像这样声明一个函数%=~
.
One way would be to declare a function %=~
like this.
`%=~%` <- function(x, y, tol = .Machine$double.eps^0.5) abs(x - y) < tol
2 %=~% (2+1e-15)
#[1] TRUE
然后可以使用您选择的公差tol
.
You could then use the tolerance tol
of your choice.
这篇关于==在R中,具有.Machine $ double.eps精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文