Observe:
(< 1/4 0.5M)
;=> true ; as expected
(< 1/3 0.5M)
; Execution error (ArithmeticException) at java.math.BigDecimal/divide (BigDecimal.java:1783).
; Non-terminating decimal expansion; no exact representable decimal result.
This is because Clojure tries to coerce the ratio to a BigDecimal, which can fail.
This worked (and produced expected results) in Clojure up to 1.2.1; the current behaviour is from 1.3.0 onwards. Not sure whether this is a bug or expected behaviour; in either case, I haven’t seen it documented.
>
, <=
, >=
all exhibit the same behaviour. =
works.
I’ve also noticed that =
can’t determine whether a ratio is equal to a bigdec, although <
and >
do sometimes produce a good answer:
((juxt < = >) 1/2 0.5M)
;=> [false false false]
((juxt < = >) 1/4 0.5M)
;=> [true false false]
The same is true of ratios and doubles:
((juxt < = >) 1/2 0.5M)
;=> [false false false]
((juxt < = >) 1/4 0.5M)
;=> [true false false]
This seems contrary to the docstring of =
, which states that „[it] compares numbers and collections in a type-independent manner”.