Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/FoundationEssentials/Decimal/Decimal+Math.swift
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,7 @@ extension Decimal {

// D1: Normalize
// Calculate d such that `d*highest_dight_of_divisor >= b/2 (0x8000)
let d = (1 << 16) / UInt32(divisor[divisor.count - 1] + 1)
let d: UInt32 = (1 << 16) / (UInt32(divisor[divisor.count - 1]) + 1)
// This is to make the whole algorithm work and
// (dividend * d) / (divisor * d) == dividend / divisor
var normalizedDividend = try self._integerMultiplyByShort(
Expand Down
27 changes: 26 additions & 1 deletion Tests/FoundationEssentialsTests/DecimalTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,32 @@ final class DecimalTests : XCTestCase {
XCTAssertTrue(Decimal._compare(lhs: expected, rhs: result) == .orderedSame)
}

func testCrashingDivision() throws {
// This test makes sure the following division
// does not crash
let first: Decimal = Decimal(1147858867)
let second: Decimal = Decimal(4294967295)
let result = first / second
let expected: Decimal = Decimal(
_exponent: -38,
_length: 8,
_isNegative: 0,
_isCompact: 1,
_reserved: 0,
_mantissa: (
58076,
13229,
12316,
25502,
15252,
32996,
11611,
5147
)
)
XCTAssertEqual(result, expected)
}

func testPower() throws {
var a = Decimal(1234)
var result = try a._power(exponent: 0, roundingMode: .plain)
Expand Down Expand Up @@ -1272,5 +1298,4 @@ final class DecimalTests : XCTestCase {
XCTAssertEqual(length, 3)
}
#endif

}