# How to compare vectors?

**URL:** https://discourse.julialang.org/t/how-to-compare-vectors/121295
**Category:** General Usage
**Tags:** question
**Created:** [October 14, 2024, 4:16pm UTC](https://discourse.julialang.org/t/how-to-compare-vectors/121295 "2024-10-14T16:16:31Z")
**Posts on this page:** 11
**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: [October 14, 2024, 4:16pm UTC](https://discourse.julialang.org/t/how-to-compare-vectors/121295/1 "2024-10-14T16:16:31Z")

</div>

What is a good way to check if two vectors are approximately equal?

I have this check that fails:

```julia
using Test
a = [1,2,3]
b = [1,2,3.0000001]
@test a ≈ b

```

?

---

<div class="post-metadata">

### Author: ![DanielVandH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielvandh/32/31134_2.png) [@DanielVandH](https://discourse.julialang.org/u/DanielVandH)
#### Post date: [October 14, 2024, 4:21pm UTC](https://discourse.julialang.org/t/how-to-compare-vectors/121295/2 "2024-10-14T16:21:02Z")

</div>

Just pass the appropriate tolerances you want for your test. See `?@test`.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [October 14, 2024, 4:28pm UTC](https://discourse.julialang.org/t/how-to-compare-vectors/121295/3 "2024-10-14T16:28:18Z")

</div>

As an aside, I’m surprised you asked this question, as you’ve been here for a while and I’ve lost count of the number of times @stevengj has explained your question. 🙂

---

<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: [October 14, 2024, 4:39pm UTC](https://discourse.julialang.org/t/how-to-compare-vectors/121295/4 "2024-10-14T16:39:19Z")

</div>

> [@DanielVandH](#):
>
> `?@test`.

Really helpful. Thank you!

For the record: This works:

```julia
using Test
a = [1,2,3]
b = [1,2,3.0000001]
@test a ≈ b atol=1e-5

```

---

<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: [October 14, 2024, 4:39pm UTC](https://discourse.julialang.org/t/how-to-compare-vectors/121295/5 "2024-10-14T16:39:49Z")

</div>

> [@rafael.guerra](#):
>
> I’m surprised you asked this question

I am getting old. And it is not easy to find search terms that allow you to find the answer to this question online.

---

<div class="post-metadata">

### Author: ![\_bernhard](https://avatars.discourse-cdn.com/v4/letter/_/bc79bd/32.png) [@\_bernhard](https://discourse.julialang.org/u/_bernhard)
#### Post date: [October 14, 2024, 4:57pm UTC](https://discourse.julialang.org/t/how-to-compare-vectors/121295/6 "2024-10-14T16:57:02Z")

</div>

I think the point was rather, that

```julia
help?> @test

```

can be expected to be the first stop for getting help of anyone even just mildly familiar with julia.

---

<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: [October 14, 2024, 4:58pm UTC](https://discourse.julialang.org/t/how-to-compare-vectors/121295/7 "2024-10-14T16:58:13Z")

</div>

Not really. How shall I know that the \approx operator is defined in the package Test? I did not expect that.

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [October 14, 2024, 4:59pm UTC](https://discourse.julialang.org/t/how-to-compare-vectors/121295/8 "2024-10-14T16:59:37Z")

</div>

See for one of the more recent ones:

> [@History of \`isapprox\` in languages](https://discourse.julialang.org/t/history-of-isapprox-in-languages/119347):
>
> Does anyone know why the de-facto standard definition of isapprox intentionally fails when one of the arguments is zero? isapprox(0.0, 0.0 + 1e-10) # false isapprox(1.0, 1.0 + 1e-10) # true It is clear from the formula why this is happening: ||x - y|| \le max(atol, rtol\*max(||x||, ||y||)) If we set y=0, we get: ||x|| \le max(atol, rtol\*||x||) The default atol = 0 then makes it: ||x|| \le rtol \* ||x|| and the default rtol = \sqrt\epsilon leads to: ||x|| \le \sqrt\epsilon ||x|| which…

---

<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: [October 14, 2024, 5:00pm UTC](https://discourse.julialang.org/t/how-to-compare-vectors/121295/9 "2024-10-14T17:00:39Z")

</div>

That was one of the few threads I did not read because it is too long.

---

<div class="post-metadata">

### Author: ![\_bernhard](https://avatars.discourse-cdn.com/v4/letter/_/bc79bd/32.png) [@\_bernhard](https://discourse.julialang.org/u/_bernhard)
#### Post date: [October 14, 2024, 5:08pm UTC](https://discourse.julialang.org/t/how-to-compare-vectors/121295/10 "2024-10-14T17:08:32Z")

</div>

But you don’t need to know that for the above either, do you?

---

<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: [October 14, 2024, 5:32pm UTC](https://discourse.julialang.org/t/how-to-compare-vectors/121295/11 "2024-10-14T17:32:05Z")

</div>

> [@ufechner7](#):
>
> How shall I know that the \approx operator is defined in the package Test? I did not expect that.

It’s not, it’s defined in `Base`. See [`Base.isapprox`](https://docs.julialang.org/en/v1/base/math/#Base.isapprox) or type `≈` or `isapprox` at the `help?>` prompt.

What is specific to the `@test` macro is that it has its own convenience syntax for passing keyword arguments to infix operators, so that you can type:

```julia
@test x ≈ y rtol=1e-3

```

instead of

```julia
@test ≈(x,y, rtol=1e-3)

```

or

```julia
@test isapprox(x, y, rtol=1e-3)

```

though all three are equivalent. The `@test` keyword syntax isn’t actually specific to `≈`, but that is probably where it is most often used.
