The “===” Operator in Julia

Written by on July 23, 2023 in Julia, Programming with 0 Comments

In addition to the conventional “==” operator that checks if two objects are equal, Julia has the “===” operator to check for equality. What is the difference between these two operators?

The “==” operator checks whether two objects have the same value. Here are some examples:

The "==" Operator

The “==” Operator

Cell 5 shows that the RHS float value is converted to int before checking for equality. Cell 8 shows that Julia handles NaN as a special value.

The “===” operator is stricter than “==”. It checks whether the two operands are bitwise equal. Here are some examples:

The "===" Operator

The “===” Operator

Look at Cells 11 and 13. The behavior is different from that of “==” operator.

Common Lisp has “=”, “eq”, “eql”, “equal”, and “equalp” for comparing two objects and is the only language that I know with such finer distinctions. Please see this article  for more details.

Other languages that have the “===” operator are PHP and Javascript. Ruby also has this operator but its semantics is different.

Finally, Julia also has “isequal()” function to compare two objects.

isequal() Function

isequal() Function

You can see that Cell 29 is similar to “==” operator and Cell 30 behaves like “===” operator.

It is clear from the above examples that the choice of “==”, “===” and “isequal” is dictated by the usage context.

Have a great weekend!

Tags: , ,


If you enjoyed this article, subscribe now to receive more just like it.

Subscribe via RSS Feed

Leave a Reply

Your email address will not be published. Required fields are marked *