Skip to content

Commit 5aa5b34

Browse files
committed
SR-13015: Decimal calculation incorrect on Linux
Fix two old typos in the overflow check in integerMultiply(), which was screwing up Decimal calculations under certain conditions.
1 parent aa07812 commit 5aa5b34

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

Sources/Foundation/Decimal.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1025,7 +1025,7 @@ fileprivate func integerMultiply<T:VariableLengthNumber>(_ big: inout T,
10251025
accumulator = UInt32(carry) + bigij + UInt32(right[j]) * UInt32(left[i])
10261026
carry = UInt16(truncatingIfNeeded:accumulator >> 16)
10271027
big[i+j] = UInt16(truncatingIfNeeded:accumulator)
1028-
} else if carry != 0 || (right[j] == 0 && left[j] == 0) {
1028+
} else if carry != 0 || !(right[j] == 0 && left[i] == 0) {
10291029
return .overflow
10301030
}
10311031
}

Tests/Foundation/Tests/TestDecimal.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ class TestDecimal: XCTestCase {
9999
XCTAssertEqual(.lossOfPrecision, NSDecimalAdd(&result, &one, &addend, .plain), "1 + 1e-39")
100100
XCTAssertEqual("1", result.description)
101101
XCTAssertEqual(.orderedSame, NSDecimalCompare(&one, &result), "1 + 1e-39")
102+
103+
let a = Decimal(string: "498.7509045")!
104+
let b = Decimal(string: "8.453441368210501065891847765109162027")!
105+
XCTAssertEqual(a + b, Decimal(string: "507.2043458682105010658918477651091")!)
102106
}
103107

104108
func test_BasicConstruction() {

0 commit comments

Comments
 (0)