[Rd] all.equal: possible mismatch between behaviour and documentation

Jon Clayden jon.clayden at gmail.com
Tue Jul 28 12:58:06 CEST 2015

Dear all,

The documentation for `all.equal.numeric` says

    Numerical comparisons for ‘scale = NULL’ (the default) are done by
    first computing the mean absolute difference of the two numerical
    vectors.  If this is smaller than ‘tolerance’ or not finite,
    absolute differences are used, otherwise relative differences
    scaled by the mean absolute difference.

But the actual behaviour of the function is to use relative
differences if the mean value of the first argument is greater than

    all.equal(0.1, 0.102, tolerance=0.01)
    # [1] "Mean relative difference: 0.02"

It seems to me that this example should produce `TRUE`, because
abs(0.1-0.102) < 0.01, but it does not, because abs(0.1) > 0.01. The
relevant section in the source seems to be

    what <- if (is.null(scale)) {
        xn <- mean(abs(target))
        if (is.finite(xn) && xn > tolerance) {
            xy <- xy/xn
        else "absolute"

I think `xy`, not `xn`, should be tested here.

The last line of the documentation, indicating that relative
differences are "scaled by the mean absolute difference" also seems
not to match the code, but in this aspect the code is surely right,
i.e., the relative difference is relative to the mean value, not the
mean difference.

All the best,

More information about the R-devel mailing list