Skip to content

Commit

Permalink
Rename function to clarify behaviour and fix bug
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
alblue committed Dec 1, 2016
1 parent 31b1737 commit e58bd86
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions Foundation/NSDecimal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ fileprivate extension UInt16 {
}
}

fileprivate func decimalCompare<T:VariableLengthNumber>(
fileprivate func mantissaCompare<T:VariableLengthNumber>(
_ left: T,
_ right: T) -> ComparisonResult {

Expand All @@ -655,7 +655,7 @@ fileprivate func decimalCompare<T:VariableLengthNumber>(
return .orderedAscending
}
let length = left._length // == right._length
for i in 0..<length {
for i in (0..<length).reversed() {
let comparison = left[i].compareTo(right[i])
if comparison != .orderedSame {
return comparison
Expand Down Expand Up @@ -1131,7 +1131,7 @@ public func NSDecimalAdd(_ result: UnsafeMutablePointer<Decimal>, _ leftOperand:
}
result.pointee._length = length
} else { // not the same sign
let comparison = decimalCompare(a,b)
let comparison = mantissaCompare(a,b)

switch comparison {
case .orderedSame:
Expand Down Expand Up @@ -1728,7 +1728,7 @@ extension Decimal {
var selfNormal = self
var otherNormal = other
_ = NSDecimalNormalize(&selfNormal, &otherNormal, .down)
let comparison = decimalCompare(selfNormal,otherNormal)
let comparison = mantissaCompare(selfNormal,otherNormal)
if selfNormal._isNegative == 1 {
if comparison == .orderedDescending {
return .orderedAscending
Expand Down

0 comments on commit e58bd86

Please sign in to comment.