How to compare vectors?

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

I have this check that fails:

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

?

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

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

Really helpful. Thank you!

For the record: This works:

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

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

1 Like

I think the point was rather, that

help?> @test

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

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

See for one of the more recent ones:

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

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

It’s not, it’s defined in Base. See 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:

@test x ≈ y rtol=1e-3

instead of

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

or

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

4 Likes