R's `all.equal`, relative vs absolute
The
all.equal()
function is useful to comparing whether two numeric objects are equivalent, but
it has a weird quirk.


You might expect that the difference should be 1. But note the “relative
difference.” The default for all.equal
is to report the difference for a call
of all.equal(x, y)
as x  y/x. This also means that all.equal(x, y)
is not
going to report the same difference as all.equal(y, x)
.
The scale=
argument makes all.equal
instead report absolute difference:


This comes in especially with the tolerance=
argument. Let’s say you want to
consider two values “equal” if they’re less than .5 apart.


That second one is clearly wrong. However, if we set the scale=
argument to 1,
the tolerance is applied on the absolute scale instead.


Home  Back to blog