# 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!

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.

7 Likes