# 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:** 1\
**Showing post:** 7

<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.

---

_[View the full topic](https://discourse.julialang.org/t/how-to-add-a-tolerance-to-isapprox/118711)._
