Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should i use `==` vs `===` vs `isequal`

Tags:

julia

I see that julia has 3 different ways to do equality.

==, ===, and isequal

Which should I use, and when?

like image 692
Lyndon White Avatar asked Sep 15 '19 15:09

Lyndon White


People also ask

Why would you use === instead of ==?

Use === if you want to compare couple of things in JavaScript, it's called strict equality, it means this will return true if only both type and value are the same, so there wouldn't be any unwanted type correction for you, if you using == , you basically don't care about the type and in many cases you could face ...

When using the strict equality operator to compare objects How do they compare?

The strict equality operators ( === and !== ) use the IsStrictlyEqual Abstract Operation to compare two operands. If the operands are of different types, return false . If both operands are objects, return true only if they refer to the same object.

What is equality comparison?

Strict equality compares two values for equality. Neither value is implicitly converted to some other value before being compared. If the values have different types, the values are considered unequal. If the values have the same type, are not numbers, and have the same value, they're considered equal.

What does === mean in JavaScript?

=== (Triple equals) is a strict equality comparison operator in JavaScript, which returns false for the values which are not of a similar type. This operator performs type casting for equality. If we compare 2 with “2” using ===, then it will return a false value.


2 Answers

Basically:

  • == when you are interested in the values of two objects: 1 == 1->true and 1 == 1.0->true
  • === when you want to make sure that two objects cannot be distinguished (including arrays pointing to different memory): 1 === 1->true but 1 === 1.0->false and A = [1, 2, 3]; B = [1, 2, 3] leads to A == B->true but A === B->false (A === A->true).
  • isequal() same as == but handles floating point numbers differently: NaN == NaN->false but isequal(NaN, NaN)->true.

A deeper discussion here.

like image 189
norok2 Avatar answered Oct 22 '22 16:10

norok2


=== is the built-in equality.

  • On primitives, it is value equality: if they have the same bit level representation then they are equal.
  • On mutable structs, it is referential equality: things are equal if they are the same memory locations.
  • On immutable structs, it is structural equality: two structures are equal if they are of the same type and all their fields are equal.

In all 3 cases this is all more or less just bit-level equality, references to memory are pointers. (But of fancyness to make the immutable struct version recursive)

Ideally, one wouldn't use this much, because it is not customizable. Sometimes though it is good to use because the optimizer can reason about it really well, so it can lead to better performance for hot loops.

== is general purpose equality

It is overloadable. For Floats it follows the IEEE rules, i.e. -0.0 == 0.0, NaN != NaN. And it follows 3 values logic rules for missing == missing resulting in missing.

if == is not defined then it falls back to ===

If you have defined ==, you need to define hash as isequal falls back to using ==, see below.

isequal is equality for purpose of dictionaries.

I don't know a much better way to put it. Things that are isequal are considered the same for purposes of Dict and Set. So you can't have two items that are isequal as distinct keys in a Dict.

Use this if you want to be sure that NaNs are equal to each other, and similarly that missings are equal to each other.

When defining isequal you also must define hash. isequal(a, b) implies hash(a) == hash(b)

If isequal is not defined, then it falls back to ==

like image 40
Lyndon White Avatar answered Oct 22 '22 16:10

Lyndon White