isequal
isequal(x, y)
Similar to ==, except treats all floating-point NaN values as equal to each other, and treats -0.0 as unequal to 0.0. The default implementation of isequal calls ==, so if you have a type that doesn't have these floating-point subtleties then you probably only need to define ==.
isequal is the comparison function used by hash tables (Dict). isequal(x,y) must imply that hash(x) == hash(y).
This typically means that if you define your own == function then you must define a corresponding hash (and vice versa). Collections typically implement isequal by calling isequal recursively on all contents.
Scalar types generally do not need to implement isequal separate from ==, unless they represent floating-point numbers amenable to a more efficient implementation than that provided as a generic fallback (based on isnan, signbit, and ==).
Examples
In the Julia programming language, the function isequal(x, y) is used to compare two values for equality. It is similar to the == operator but has some additional behavior for floating-point numbers. Here are some examples of how to use the isequal function:
-
Compare integers:
julia> isequal(10, 10) true julia> isequal(10, 20) false -
Compare floating-point numbers:
julia> isequal(1.0, 1.0) true julia> isequal(1.0, 1.000001) falseThe
isequalfunction treats all floating-pointNaNvalues as equal to each other and considers-0.0as unequal to0.0. -
Compare strings:
julia> isequal("hello", "hello") true julia> isequal("hello", "world") false -
Custom types: If you define your own custom types, you may need to implement both
==andisequalfunctions. The default implementation ofisequalcalls==, so if your type doesn't have any special floating-point behavior, defining==should be sufficient.Note:
isequal(x, y)implies thathash(x) == hash(y). If you define your own==function, you should also define a correspondinghashfunction.Collections typically implement
isequalby callingisequalrecursively on all their elements.
Common mistake example:
julia> isequal(1, 1.0)
false
In this example, the mistake is comparing an integer (1) with a floating-point number (1.0). It's important to ensure that the types of the values being compared are compatible to get accurate results with isequal.
See Also
BigFloat, BigInt, Dict, eltype, fieldtype, Float32, Float64, IntSet, isa, isalnum, isalpha, isascii, iseltype, isequal, isgraph, isimmutable, isinteractive, isleaftype, isnull, ispunct, isspace, issubtype, keytype, Nullable, NullException, promote_type, typeintersect, typejoin, typemax, typemin, typeof, Val, valtype,User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.