Why is it that typeof(a) == typeof(v) is true and typeof(a) === typeof(v) is also true, but a == v is true and a === v is false?
julia> a = Array([1,2,3])
3-element Vector{Int64}:
 1
 2
 3
julia> v = Vector([1,2,3])
3-element Vector{Int64}:
 1
 2
 3
julia> typeof(a) == typeof(v)
true
julia> typeof(a) === typeof(v)
true
julia> a == v
true
julia> a === v
false
                === determines whether compared objects are identical, in the sense that no program could distinguish them.
So typeof(a) and typeof(v) are identical (the type is Vector{Int64} (alias for Array{Int64, 1}) in both cases) so they compare with === as true.
However a and v, although they have the same contents do not have the same memory location (you will likely get different values):
julia> pointer(a)
Ptr{Int64} @0x000001e6025161b0
julia> pointer(v)
Ptr{Int64} @0x000001e602524f90
so they are distinguishable, thus different when compared by ===.
The == is different as it considers just the equality of values. In this case a and v are arrays of the same shape and storing the same numbers, so comparing them using == returns true.
In a sense == is less strict than === when doing a comparison.
Note that e.g. if you used tuples instead of arrays:
julia> t1 = (1, 2, 3)
(1, 2, 3)
julia> t2 = (1, 2, 3)
(1, 2, 3)
julia> t1 === t2
true
The reason is that tuples are not mutable so even if you create them twice they are in this case considered identical when being compared with === (they are not distinguishable).
Finally notice that if you write:
julia> a2 = a
3-element Vector{Int64}:
 1
 2
 3
julia> a2 === a
true
The reason is that a2 and a variables point to the same array (i.e. array having the same location in memory).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With