Completed
Details
Assignee
UnassignedUnassignedReporter
importimportLabels
Approval
OkPatch
Code and TestPriority
MajorAffects versions
Fix versions
Details
Details
Assignee
Unassigned
UnassignedReporter
import
importLabels
Approval
Ok
Patch
Code and Test
Priority

Affects versions
Fix versions
Created November 30, 2012 at 7:36 PM
Updated November 23, 2013 at 1:06 AM
Resolved November 23, 2013 at 1:06 AM
BigDecimal does not have consistent comparison semantics with other numeric types.
user> *clojure-version* {:major 1, :minor 5, :incremental 1, :qualifier nil} user> (== 2.0 2.0M) true user> (== 2 2.0M) false <-- this one is not like the others user> (== 2 2.0) true user> (== 2N 2.0) true user> (== 2 (double 2.0M)) true user> (== 1.0M 1.00M) false ;; potentially surprising
Patch: clj-1118-v7.patch
Approach: Change equiv for BigDecimals so that instead of using
BigDecimal.equals()
, it usesBigDecimal.compareTo()
and checks the return value is equal to 0.The javadoc for these methods explicitly states that
BigDecimal.equals()
will treat values that are otherwise equal numerically, but differ in scale, as not equal. They also say thatBigDecimal.compareTo()
will return 0 for such BigDecimals.Doing this also changes the behavior of = when comparing BigDecimal values to other numbers. hash should be consistent with =, so now hash should return the same value for all numerically equal BigDecimal values. This patch modifies
hasheq()
to return the same value for all numerically equal BigDecimal values, by callingBigDecimal.stripTrailingZeros()
on them before hashing. This change also will make 1.0M == 1.00M which was not true before.Screened by: Alex Miller