# How to add a tolerance to isapprox

**URL:** https://discourse.julialang.org/t/how-to-add-a-tolerance-to-isapprox/118711
**Category:** General Usage
**Tags:** question
**Created:** [August 28, 2024, 1:02pm UTC](https://discourse.julialang.org/t/how-to-add-a-tolerance-to-isapprox/118711 "2024-08-28T13:02:31Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [August 28, 2024, 1:02pm UTC](https://discourse.julialang.org/t/how-to-add-a-tolerance-to-isapprox/118711/1 "2024-08-28T13:02:32Z")

</div>

I have the following line of code:

```julia
@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?

---

<div class="post-metadata">

### Author: ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)
#### Post date: [August 28, 2024, 1:05pm UTC](https://discourse.julialang.org/t/how-to-add-a-tolerance-to-isapprox/118711/2 "2024-08-28T13:05:53Z")

</div>

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

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

```

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [August 28, 2024, 1:06pm UTC](https://discourse.julialang.org/t/how-to-add-a-tolerance-to-isapprox/118711/3 "2024-08-28T13:06:22Z")

</div>

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

---

<div class="post-metadata">

### Author: ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)
#### Post date: [August 28, 2024, 1:08pm UTC](https://discourse.julialang.org/t/how-to-add-a-tolerance-to-isapprox/118711/4 "2024-08-28T13:08:28Z")

</div>

```julia
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

```

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [August 28, 2024, 1:09pm UTC](https://discourse.julialang.org/t/how-to-add-a-tolerance-to-isapprox/118711/5 "2024-08-28T13:09:41Z")

</div>

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

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [August 28, 2024, 1:17pm UTC](https://discourse.julialang.org/t/how-to-add-a-tolerance-to-isapprox/118711/6 "2024-08-28T13:17:44Z")

</div>

Thank you so much, that works! 😀

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [August 28, 2024, 1:18pm UTC](https://discourse.julialang.org/t/how-to-add-a-tolerance-to-isapprox/118711/7 "2024-08-28T13:18:18Z")

</div>

> [@LaurentPlagne](#):
>
> `all(isapprox.(a,b))`

> [@juliohm](#):
>
> Notice that the `isapprox` function has methods with vectors as well, in which case it relies on approximate norms.

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
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
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](https://docs.julialang.org/en/v1/base/math/#Base.isapprox) on `x ≈ 0` comparisons.

As I wrote [in another thread](https://discourse.julialang.org/t/approximate-equality/8952/41), 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.
