Skip to content

Commit ac4445d

Browse files
authored
Merge pull request #3074 from xwu/decimal-significand-again
[SR-15156] Another fix for `Decimal(sign:exponent:significand:)`.
2 parents e2219b9 + 2b8dabf commit ac4445d

File tree

4 files changed

+6
-2
lines changed

4 files changed

+6
-2
lines changed

Darwin/Foundation-swiftoverlay-Tests/TestDecimal.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,8 +580,10 @@ class TestDecimal : XCTestCase {
580580

581581
let a = Decimal.leastNonzeroMagnitude
582582
XCTAssertEqual(Decimal(sign: .plus, exponent: -10, significand: a), 0)
583+
XCTAssertEqual(Decimal(sign: .plus, exponent: .min, significand: a), 0)
583584
let b = Decimal.greatestFiniteMagnitude
584585
XCTAssertTrue(Decimal(sign: .plus, exponent: 10, significand: b).isNaN)
586+
XCTAssertTrue(Decimal(sign: .plus, exponent: .max, significand: b).isNaN)
585587
}
586588

587589
func test_SimpleMultiplication() {

Darwin/Foundation-swiftoverlay/Decimal.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ extension Decimal {
519519
public init(sign: FloatingPointSign, exponent: Int, significand: Decimal) {
520520
self = significand
521521
let error = withUnsafeMutablePointer(to: &self) {
522-
NSDecimalMultiplyByPowerOf10($0, $0, Int16(exponent), .plain)
522+
NSDecimalMultiplyByPowerOf10($0, $0, Int16(clamping: exponent), .plain)
523523
}
524524
if error == .underflow { self = 0 }
525525
// We don't need to check for overflow because `Decimal` cannot represent infinity.

Sources/Foundation/Decimal.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ extension Decimal {
687687
public init(sign: FloatingPointSign, exponent: Int, significand: Decimal) {
688688
self = significand
689689
let error = withUnsafeMutablePointer(to: &self) {
690-
NSDecimalMultiplyByPowerOf10($0, $0, Int16(exponent), .plain)
690+
NSDecimalMultiplyByPowerOf10($0, $0, Int16(clamping: exponent), .plain)
691691
}
692692
if error == .underflow { self = 0 }
693693
// We don't need to check for overflow because `Decimal` cannot represent infinity.

Tests/Foundation/Tests/TestDecimal.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,8 +821,10 @@ class TestDecimal: XCTestCase {
821821

822822
let a = Decimal.leastNonzeroMagnitude
823823
XCTAssertEqual(Decimal(sign: .plus, exponent: -10, significand: a), 0)
824+
XCTAssertEqual(Decimal(sign: .plus, exponent: .min, significand: a), 0)
824825
let b = Decimal.greatestFiniteMagnitude
825826
XCTAssertTrue(Decimal(sign: .plus, exponent: 10, significand: b).isNaN)
827+
XCTAssertTrue(Decimal(sign: .plus, exponent: .max, significand: b).isNaN)
826828
}
827829

828830
func test_SimpleMultiplication() {

0 commit comments

Comments
 (0)