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:
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:
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.
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!
Recent Comments