Test: absolute tolerance for vectors

Does anyone know how tolerances are computed for vectors in the Base.Test environment? I’m seeing some behavior that I can’t fully explain, see below.

using Test

a = rand(10) * 1e-4

@test a ≈ zeros(size(a)) atol=1e-4

Which returns:

Test Failed at REPL[16]:1
  Expression: ≈(a, zeros(size(a)), atol=0.0001)
   Evaluated: [7.34438e-5, 1.48842e-5, 1.86573e-5, 3.26998e-5, 6.13959e-5, 4.73754e-5, 3.91773e-5, 6.14291e-5, 7.24425e-6, 4.73723e-5] ≈ [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] (atol=0.0001)

To me it seems like that test should have passed? Any ideas?

See the documentation, eg ?isapprox in the REPL. In particular,

x and y may also be arrays of numbers, in which case norm defaults to the usual
norm function in LinearAlgebra, but may be changed by passing a norm::Function keyword argument. (For numbers, norm is the same thing as abs.) When x and y are arrays, if norm(x-y) is not finite (i.e. ±Inf or NaN), the comparison falls back to checking whether all elements of x and y are approximately equal component-wise.

1 Like