[Rd] all.equal: possible mismatch between behaviour and documentation
Jon Clayden
jon.clayden at gmail.com
Tue Jul 28 13:14:48 CEST 2015
Sorry; minor clarification. The actual test criterion in the example I
gave is of course abs((0.1-0.102)/0.1) < 0.01, not abs(0.1) < 0.01. In
any case, this does not match (my reading of) the docs, and the result
is not `TRUE`.
Regards,
Jon
On 28 July 2015 at 11:58, Jon Clayden <jon.clayden at gmail.com> wrote:
> 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
> `tolerance`:
>
> 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
> "relative"
> }
> 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,
> Jon
More information about the R-devel
mailing list