Skip to content

Commit 13b563e

Browse files
authored
Add runtime check to Decimal(sign:exponent:significand:)) for bin compat (#933)
1 parent 706ef7f commit 13b563e

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

Sources/FoundationEssentials/Decimal/Decimal+Conformances.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,21 @@ extension Decimal /* : FloatingPoint */ {
236236
}
237237

238238
public init(sign: FloatingPointSign, exponent: Int, significand: Decimal) {
239+
#if FOUNDATION_FRAMEWORK
240+
// Compatibility path
241+
if Self.compatibility1 {
242+
self.init(
243+
_exponent: Int32(exponent) + significand._exponent,
244+
_length: significand._length,
245+
_isNegative: sign == .plus ? 0 : 1,
246+
_isCompact: significand._isCompact,
247+
_reserved: 0,
248+
_mantissa: significand._mantissa
249+
)
250+
return
251+
}
252+
#endif
253+
239254
self = significand
240255
do {
241256
self = try significand._multiplyByPowerOfTen(

Tests/FoundationEssentialsTests/DecimalTests.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,9 +1159,21 @@ final class DecimalTests : XCTestCase {
11591159
var x = -42 as Decimal
11601160
XCTAssertEqual(x.significand.sign, .plus)
11611161
var y = Decimal(sign: .plus, exponent: 0, significand: x)
1162+
#if FOUNDATION_FRAMEWORK
1163+
if Decimal.compatibility1 {
1164+
XCTAssertEqual(y, 42)
1165+
y = Decimal(sign: .minus, exponent: 0, significand: x)
1166+
XCTAssertEqual(y, -42)
1167+
} else {
1168+
XCTAssertEqual(y, -42)
1169+
y = Decimal(sign: .minus, exponent: 0, significand: x)
1170+
XCTAssertEqual(y, 42)
1171+
}
1172+
#else
11621173
XCTAssertEqual(y, -42)
11631174
y = Decimal(sign: .minus, exponent: 0, significand: x)
11641175
XCTAssertEqual(y, 42)
1176+
#endif
11651177

11661178
x = 42 as Decimal
11671179
XCTAssertEqual(x.significand.sign, .plus)

0 commit comments

Comments
 (0)