Skip to content

Commit e58bd86

Browse files
committed
Rename function to clarify behaviour and fix bug
The decimalCompare function could be confused with the NSDecimalCompare function, but it does something very different. Since this function only compares the mantissa, rename it to compareMantissa instead. Also fix a bug which slipped through whereby the mantissa would be compared in the wrong order, leading to some incorrect results when comparing Decimal values.
1 parent 31b1737 commit e58bd86

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

Foundation/NSDecimal.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ fileprivate extension UInt16 {
644644
}
645645
}
646646

647-
fileprivate func decimalCompare<T:VariableLengthNumber>(
647+
fileprivate func mantissaCompare<T:VariableLengthNumber>(
648648
_ left: T,
649649
_ right: T) -> ComparisonResult {
650650

@@ -655,7 +655,7 @@ fileprivate func decimalCompare<T:VariableLengthNumber>(
655655
return .orderedAscending
656656
}
657657
let length = left._length // == right._length
658-
for i in 0..<length {
658+
for i in (0..<length).reversed() {
659659
let comparison = left[i].compareTo(right[i])
660660
if comparison != .orderedSame {
661661
return comparison
@@ -1131,7 +1131,7 @@ public func NSDecimalAdd(_ result: UnsafeMutablePointer<Decimal>, _ leftOperand:
11311131
}
11321132
result.pointee._length = length
11331133
} else { // not the same sign
1134-
let comparison = decimalCompare(a,b)
1134+
let comparison = mantissaCompare(a,b)
11351135

11361136
switch comparison {
11371137
case .orderedSame:
@@ -1728,7 +1728,7 @@ extension Decimal {
17281728
var selfNormal = self
17291729
var otherNormal = other
17301730
_ = NSDecimalNormalize(&selfNormal, &otherNormal, .down)
1731-
let comparison = decimalCompare(selfNormal,otherNormal)
1731+
let comparison = mantissaCompare(selfNormal,otherNormal)
17321732
if selfNormal._isNegative == 1 {
17331733
if comparison == .orderedDescending {
17341734
return .orderedAscending

0 commit comments

Comments
 (0)