Stream: helpdesk (published)

Topic: Differences between ==, ===, isequal


view this post on Zulip Júlio Hoffimann (Feb 01 2021 at 01:12):

Can you please elaborate on the differences between ==, === and isequal? If you had to explain the differences to a newcomer, how would you do it? Can you also give some advise for developers who wish to implement "equality" for their own types?

view this post on Zulip Mason Protter (Feb 01 2021 at 01:31):

I would say something along the lines of

There are various notions of equality that are useful for various circumstances. Sometimes we want to be able to distinguish between 1.0 and 1 for instance. Sometimes, we think 'yes, these two objects represent the same concept' but other times we want to be able to say 'well, even if they're closely related, they're nor literally identically the same'

This is where == versus === comes in. == is sometimes called 'equality' whereas === means 'egality' (from German, egal)

julia> 1 == 1.0
true

julia> 1 === 1.0
false

The difference between == and isequal` is a bit more subtle, but they're usually the same, except for with Floating point numbers where they behave a bit different for legacy compatability reasons

view this post on Zulip Benoit Pasquier (Feb 01 2021 at 01:35):

Maybe extend the example to 1, 1.0, and NaN? As in

julia> vals = Any[1, 1.0, NaN]
3-element Array{Any,1}:
   1
   1.0
 NaN

julia> [isequal(x,y) for x in vals, y in vals]
3×3 Array{Bool,2}:
 1  1  0
 1  1  0
 0  0  1

julia> [x == y for x in vals, y in vals]
3×3 Array{Bool,2}:
 1  1  0
 1  1  0
 0  0  0

julia> [x === y for x in vals, y in vals]
3×3 Array{Bool,2}:
 1  0  0
 0  1  0
 0  0  1

view this post on Zulip Mason Protter (Feb 01 2021 at 01:39):

Two nans helps a bit actually:

julia> vals = Any[1, 1.0, NaN, NaN]
4-element Vector{Any}:
   1
   1.0
 NaN
 NaN

julia> [isequal(x,y) for x in vals, y in vals]
4×4 Matrix{Bool}:
 1  1  0  0
 1  1  0  0
 0  0  1  1
 0  0  1  1

julia> [x == y for x in vals, y in vals]
4×4 Matrix{Bool}:
 1  1  0  0
 1  1  0  0
 0  0  0  0
 0  0  0  0

julia> [x === y for x in vals, y in vals]
4×4 Matrix{Bool}:
 1  0  0  0
 0  1  0  0
 0  0  1  1
 0  0  1  1

view this post on Zulip Benoit Pasquier (Feb 01 2021 at 01:42):

I'm not sure I see why :sweat_smile:

view this post on Zulip Benoit Pasquier (Feb 01 2021 at 01:44):

Oh I know think I get it, what about this:

julia> vals = Any[1, 1.0, NaN, 0/0]
4-element Array{Any,1}:
   1
   1.0
 NaN
 NaN

julia> [isequal(x,y) for x in vals, y in vals]
4×4 Array{Bool,2}:
 1  1  0  0
 1  1  0  0
 0  0  1  1
 0  0  1  1

julia> [x == y for x in vals, y in vals]
4×4 Array{Bool,2}:
 1  1  0  0
 1  1  0  0
 0  0  0  0
 0  0  0  0

julia> [x === y for x in vals, y in vals]
4×4 Array{Bool,2}:
 1  0  0  0
 0  1  0  0
 0  0  1  0
 0  0  0  1

view this post on Zulip Mason Protter (Feb 01 2021 at 01:48):

Yeah, isequal treats all various NaN values (of which there are many!) the same.

view this post on Zulip Benoit Pasquier (Feb 01 2021 at 01:52):

BTW, are you sure the origin for egal is not french? (Could not find the flag emojis!)

view this post on Zulip Mason Protter (Feb 01 2021 at 01:56):

Ah I didn't realize this was also in french. Looks like it's a loan word in German from French!

view this post on Zulip Benoit Pasquier (Feb 01 2021 at 05:40):

I have no idea, I just asked because we also say "égal" in french, like in the french moto "Liberté, Égalité, Fraternité"

view this post on Zulip Florian Große (Feb 01 2021 at 08:40):

You should add a missing, too. missing === missing but not missing == missing, which is... missing ^^

view this post on Zulip Júlio Hoffimann (Feb 01 2021 at 09:11):

Yes it would be nice to add missing to the great code snippets above.

view this post on Zulip Benoit Pasquier (Feb 01 2021 at 10:45):

Oh wait, is it better to edit my post above (because it is published now?) or should I just reply?

view this post on Zulip Michael Abbott (Feb 01 2021 at 11:41):

Maybe it's worth including minus zero? vals = Any[0, 0.0, -0.0, NaN, 0/0, missing]. Would actually be great to put this in the docs somewhere.

view this post on Zulip Rein Zustand (Feb 02 2021 at 01:55):

Strange. Egality seems to be closer in meaning to equivalence. I'd inclined to think that 1 is equivalent to 1.0, but not equal to 1.0.


Last updated: Oct 02 2023 at 04:34 UTC