Clojure

Documentation for "=" is misleading

Details

  • Type: Enhancement Enhancement
  • Status: Open Open
  • Priority: Minor Minor
  • Resolution: Unresolved
  • Affects Version/s: Release 1.5
  • Fix Version/s: None
  • Component/s: None
  • Labels:
  • Environment:
    linux 2.6.32-431.el6.x86_64

Description

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;

clojure.core/=
([x] [x y] [x y & more])
Equality. Returns true if x equals y, false if not. Same as
Java x.equals 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,
comparison.

Activity

Hide
Alex Miller added a comment -

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

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

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)

Show
George added a comment - 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)
Hide
Gary Fredericks added a comment -

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.

Show
Gary Fredericks added a comment - 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.
Hide
Kevin Downey added a comment -

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)))

etc.

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.

Show
Kevin Downey added a comment - 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))) etc. 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.

People

  • Assignee:
    Unassigned
    Reporter:
    George
Vote (0)
Watch (3)

Dates

  • Created:
    Updated: