store/[org.clojure/clojure "1.8.0"] clj::clojure.core/==

Community Documentation

Edit

(== x y) is true if x and y are both numbers, and represent numerically equal values. Unlike =, there are no separate 'categories' of numeric values that are treated as always unequal to each other. If you call == with more than two arguments, the result will be true when all consecutive pairs are ==. An exception is thrown if any argument is not a numeric value.

Exceptions, or possible surprises:

  • == is false for BigDecimal values with different scales, e.g. (== 1.50M 1.500M) is false. http://dev.clojure.org/jira/browse/CLJ-1118
  • 'Not a Number' values Float/NaN and Double/NaN are not equal to any value, not even themselves.

Source

(defn ==
  "Returns non-nil if nums all have the equivalent
  value (type-independent), otherwise false"
  {:inline (fn [x y] `(. clojure.lang.Numbers (equiv ~x ~y)))
   :inline-arities #{2}
   :added "1.0"}
  ([x] true)
  ([x y] (. clojure.lang.Numbers (equiv x y)))
  ([x y & more]
   (if (== x y)
     (if (next more)
       (recur y (first more) (next more))
       (== y (first more)))
     false)))

Example 1

Edit
;; Some equality exceptions noted by AndyF
(= 2 2.0)   ; = has different categories integer and floating point
;; => false

(== 2 2.0)  ; but == sees same numeric value
;; => true

(== 5 5N (float 5.0) (double 5.0) (biginteger 5))
;; => true

(== 5 5.0M) ; this is likely a bug
;; => false

(== Double/NaN Double/NaN)  ; this is normal
;; => false

(== 2 "a")
;; => ClassCastException java.lang.String cannot be cast to java.lang.Number  clojure.lang.Numbers.equiv (Numbers.java:206)

Example 2

Edit
;; true:
(== 1)
(== 1 1)       
(== 1/1, 2/2, 3/3, 4/4)   
(== 1, 1.0, 1/1)
(== :foo)

;; false:
(== 1 2)
(== 1 \1)
(== 1 "1")

Uses on crossclj