ClojureDocs

导航

名称空间

==

clojure.core

从 1.0 开始提供 ()
  • (== x)
  • (== x y)
  • (== x y & more)
Returns non-nil if nums all have the equivalent
value (type-independent), otherwise false
6 Examples
;; true:
(== 1)
(== 1 1)       
(== 1/1, 2/2, 3/3, 4/4)   
(== 1, 1.0, 1/1)
(== :foo)


;; false:
(== 1 2)

;; ClassCastException
(== 1 \1)
(== 1 "1")
user=> (= 0.0 0)
false
user=> (== 0.0 0)
true
;; Just what you would expect
(== 2.0 1.9999999)
;;=> false

;; a suprising result
(== 2.0 2 6/3 1.9999999999999999)
;;=> true ??!?
;; Yes, there is some rounding off going on.
;; if you take off just one of the repeating 9 (on my machine) these compare.

;; When floating point numbers are far enough from each other
(== 2.0 1.9999999)
;;=> false
(- 100.0 100.00000000000001)  ;13(Thirteen) 0s after floating point in the last number
;;=> -1.4210854715202004E-14

;; When two floating point numbers are too close some basic algebraic properties don't strictly hold.
(== 2.0 1.9999999999999999)
;;=> true

(* 100 (- 1.0 1.0000000000000001))  ;15(fifteen) 0s after floating point in the last number
;;=> 0.0

;; They are still different types
(= 2 1.9999999999999999)
;;=> false

;; see more from https://wikibooks.cn/wiki/Floating_Point/Epsilon
;; I found above example was distracting by putting 6/3 and 2 in the equality check that is why I decided to write up a similar but new example.
;; See the Clojure Equality guide for more details:
;; https://clojure.org/guides/equality

;; == returns false whenever comparing the special "not a number" value to any
;; number, including itself.

user=> (== 1 ##NaN)
false               ;; this result you probably expect

user=> (== ##NaN ##NaN)
false               ;; this one may surprise you
user => (= 1/2 0.5)
;; => false

user => (== 1/2 0.5)
;; => true
See Also

Equality. Returns true if x equals y, false if not. Same as Java x.equals(y) except it also works ...

Added by gstamp

Tests if 2 arguments are the same object

Added by dansalmo

Comparator. Returns a negative number, zero, or a positive number when x is logically 'less than',...

Added by jafingerhut
3 Notes
    By , created 14.5 years ago

    There is a difference between "=" and "==". For primitives you definitely want to use "==" as "=" will result in a cast to the wrapped types for it's arguments.

    This may not be the case come Clojure 1.3 (see [1])

    [1] http://github.com/clojure/clojure/commit/df8c65a286e90e93972bb69392bc106128427dde

    By , created 12.1 years ago

    So what is difference with =?

    By , created 12.1 years ago

    '== is defined only for numbers, where '= is general equality. The example showing (== :foo) as true is a bit misleading because (== :foo :foo) produces an exception. Unary == always returns true as an optimization.