Reducing Precision in Doubles in R

前端 未结 3 2009
广开言路
广开言路 2021-01-14 05:28

I\'m looking for a way to consistently ignore small differences between floating point numbers in R (these are double precision floating points as per IEC 60559), by using b

3条回答
  •  隐瞒了意图╮
    2021-01-14 06:32

    Warning: this is not an direct answer to the question.

    I find the following two functions useful. They allow me to compare doubles to a given degree of precision.

    are.equal <- function(x, y, tol = .Machine$double.eps^0.5) abs(x - y) < tol
    is.zero <- function(x, tol = .Machine$double.eps^0.5) abs(x) < tol
    
    are.equal(1.45 - 0.55, 2.45 - 1.55)
    #[1] TRUE
    
    is.zero(1.45 - 0.55 - (2.45 - 1.55))
    #[1] TRUE
    

提交回复
热议问题