Welcome! Please see the About page for a little more info on how this works.

0 votes
in Docs by

Document for clojure.core/= says it compares numbers in a type-independent manner. In reality the comparission is made in a type dependent manner. If the above statement was true then (= 1 1.0) would eval to true not false;

((link: x) (link: x y) (link: x y & more))
Equality. Returns true if x equals y, false if not. Same as
Java x.equals(y) except it also works for nil, and compares
numbers and collections in a type-independent manner. Clojure's immutable data
structures define equals() (and thus =) as a value, not an identity,

5 Answers

0 votes
_Comment made by: hiredman_

I think this is a little more complex than described.

= does compare things in a jvm type independent manner, but it does use what people have taken to calling "equality classes"

(= [1 2] '(1 2))

(= {:a 1} (doto (java.util.HashMap.) (.put :a 1)))


now for numbers, it seems logical to me, to have floating point and precise numbers in distinct equality classes

in which case, 1.0 and 1 are in distinct equality classes, so not equal.
0 votes

Comment made by: gfredericks

The docstring is definitely misleading for people unfamiliar with this sort of thing though. Numbers are probably the first thing that the words "type independent manner" bring to mind. A brief pointer to the {{==}} function might be useful.

0 votes

Comment made by: bordatoue

I find == function to be confusing
For example
(== 1 1.0) => true
(== 1 1.0M) => false ; what is wrong with this comparison?

and doc says:
Returns non-nil if nums all have the equivalent value (type-independent)

0 votes

Comment made by: alexmiller

@George - that last example (== 1 1.0M) is actually a bug that is fixed in 1.6 where it will return true.

0 votes
Reference: https://clojure.atlassian.net/browse/CLJ-1333 (reported by alex+import)