I often end up in situations where it is necessary to check if the obtained difference is above machine precision. Seems like for this purpose R has a handy variable: .Mac
Definition of a machine.eps: it is the lowest value eps
for which 1+eps
is not 1
As a rule of thumb (assuming a floating point representation with base 2):
This eps
makes the difference for the range 1 .. 2,
for the range 2 .. 4 the precision is 2*eps
and so on.
Unfortunately, there is no good rule of thumb here. It's entirely determined by the needs of your program.
In R we have all.equal as a built in way to test approximate equality. So you could use maybe something like (x
i <- 0.1
i <- i + 0.05
i
if(isTRUE(all.equal(i, .15))) { #code was getting sloppy &went to multiple lines
cat("i equals 0.15\n")
} else {
cat("i does not equal 0.15\n")
}
#i equals 0.15
Google mock has a number of floating point matchers for double precision comparisons, including DoubleEq
and DoubleNear
. You can use them in an array matcher like this:
ASSERT_THAT(vec, ElementsAre(DoubleEq(0.1), DoubleEq(0.2)));
Update:
Numerical Recipes provide a derivation to demonstrate that using a one-sided difference quotient, sqrt
is a good choice of step-size for finite difference approximations of derivatives.
The Wikipedia article site Numerical Recipes, 3rd edition, Section 5.7, which is pages 229-230 (a limited number of page views is available at http://www.nrbook.com/empanel/).
all.equal(target, current,
tolerance = .Machine$double.eps ^ 0.5, scale = NULL,
..., check.attributes = TRUE)
These IEEE floating point arithmetic is a well known limitation of computer arithmetic and is discussed in several places:
.
dplyr::near()
is another option for testing if two vectors of floating point numbers are equal.
The function has a built in tolerance parameter: tol = .Machine$double.eps^0.5
that can be adjusted. The default parameter is the same as the default for all.equal()
.