How to add a tolerance to isapprox

I have the following line of code:

@test all(forces[i,:] .β‰ˆ kps4.forces[i])

I would like to increase the relative tolerance, in other words add the parameter rtol=1e-4 . How can I do that?

I think that you have to use the function form of isapprox

 isapprox(x, y; atol::Real=0, rtol::Real=atol>0 ? 0 : √eps, nans::Bool=false[, norm::Function])

The point is, I do not know how to apply this function to a pair of vectors.

julia> a=[1,2,3]
3-element Vector{Int64}:
 1
 2
 3

julia> b=[1,2,3.000001]
3-element Vector{Float64}:
 1.0
 2.0
 3.000001


julia> all(isapprox.(a,b))
false

julia> all(isapprox.(a,b,rtol=1.e-2))
true

Notice that the isapprox function has methods with vectors as well, in which case it relies on approximate norms.

3 Likes

Thank you so much, that works! :grinning:

1 Like

I would strongly recommend using the vector form in general, i.e. isapprox(a,b) rather than all(isapprox.(a,b)).

Elementwise approximate comparison discards information about the scale that can be obtained from the other elements. For example, intuitively you would expect:

\begin{bmatrix} 1 \\ 0 \end{bmatrix} \approx \begin{bmatrix} 1 \\ 10^{-100} \end{bmatrix}

to return true, and indeed the vector form of isapprox does this:

julia> [1, 0] β‰ˆ [1, 1e-100]
true

because the difference 1e-100 is small (according the default rtol=√Ρ) compared to the length of the vector (β‰ˆ 1). However, if you do an elementwise isapprox, then it fails:

julia> all([1, 0] .β‰ˆ [1, 1e-100])
false

because 0 β‰ˆ 1e-100 correctly returns false: the two numbers have no significant digits in common, and there is no absolute reference scale by which we can say that 1e-100 is β€œsmall” (compared to what)? See also the isapprox docstring on x β‰ˆ 0 comparisons.

As I wrote in another thread, people often get approximate equality wrong on their first try. Even NumPy got it wrong with their broken isclose.

PS. If you think this would be solved by having a default absolute tolerance, consider [1e100, 0] β‰ˆ [1e100, 1], which should also be true in a relative-error sense.

10 Likes