Skip to contents

Check for equality within a tolerance level

Usage

equalish(x, y, tolerance = .Machine$double.eps^0.5)

equalish_frac(x, y, tolerance = .Machine$double.eps^0.5)

Arguments

x, y

numeric vectors

tolerance

numeric scalar. tolerance level (absolute value). Defaults to .Machine$double.eps^0.5 which is a sensible default for comparing floating point numbers.

Value

equalish() returns TRUE if the absolute difference between x and y is less than tolerance.

equalish_frac() returns TRUE if the relative difference between x and y is smaller than tolerance. The relative difference is defined as abs(x - y) / pmax(abs(x), abs(y)). If both x and y are 0 the relative difference is not defined, but this function will still return TRUE.

See also

Examples

a <- 0.7
b <- 0.2
a - b == 0.5
#> [1] FALSE
equalish(a - b, 0.5)
#> [1] TRUE
equalish_frac(1000, 1010, tolerance = 0.01)
#> [1] TRUE
equalish_frac(1000, 1010, tolerance = 0.009)
#> [1] FALSE
equalish_frac(0, 0)
#> [1] TRUE