Skip to content

Commit fa71c3d

Browse files
committed
Decimal: Modernize hashing
1 parent 86a038f commit fa71c3d

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

Foundation/Decimal.swift

+6-2
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,12 @@ extension Decimal : Hashable, Comparable {
286286
return Int64(bitPattern: uint64Value)
287287
}
288288

289-
public var hashValue: Int {
290-
return Int(bitPattern: __CFHashDouble(doubleValue))
289+
public func hash(into hasher: inout Hasher) {
290+
// FIXME: This is a weak hash. We should rather normalize self to a
291+
// canonical member of the exact same equivalence relation that
292+
// NSDecimalCompare implements, then simply feed all components to the
293+
// hasher.
294+
hasher.combine(doubleValue)
291295
}
292296

293297
public static func ==(lhs: Decimal, rhs: Decimal) -> Bool {

TestFoundation/TestDecimal.swift

+4-3
Original file line numberDiff line numberDiff line change
@@ -363,9 +363,10 @@ class TestDecimal: XCTestCase {
363363
XCTAssertFalse(Decimal.nan.isTotallyOrdered(belowOrEqualTo: Decimal(2.3)))
364364
XCTAssertTrue(Decimal(2) < Decimal(3))
365365
XCTAssertTrue(Decimal(3) > Decimal(2))
366-
#if !arch(arm)
367-
XCTAssertEqual(3275573729074, Decimal(1234).hashValue)
368-
#endif
366+
367+
// FIXME: This test is of questionable value. We should test hash properties.
368+
XCTAssertEqual((1234 as Double).hashValue, Decimal(1234).hashValue)
369+
369370
XCTAssertEqual(Decimal(-9), Decimal(1) - Decimal(10))
370371
XCTAssertEqual(Decimal(3), Decimal(2).nextUp)
371372
XCTAssertEqual(Decimal(2), Decimal(3).nextDown)

0 commit comments

Comments
 (0)