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?
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
and1
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
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
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
I'm not sure I see why :sweat_smile:
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
Yeah, isequal
treats all various NaN
values (of which there are many!) the same.
BTW, are you sure the origin for egal is not french? (Could not find the flag emojis!)
Ah I didn't realize this was also in french. Looks like it's a loan word in German from French!
I have no idea, I just asked because we also say "égal" in french, like in the french moto "Liberté, Égalité, Fraternité"
You should add a missing
, too. missing === missing
but not missing == missing
, which is... missing
^^
Yes it would be nice to add missing to the great code snippets above.
Oh wait, is it better to edit my post above (because it is published now?) or should I just reply?
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.
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: Nov 22 2024 at 04:41 UTC