Skip to content

Add runtime check to Decimal(sign:exponent:significand:) for bin compat #933

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
15 changes: 15 additions & 0 deletions Sources/FoundationEssentials/Decimal/Decimal+Conformances.swift
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,21 @@ extension Decimal /* : FloatingPoint */ {
}

public init(sign: FloatingPointSign, exponent: Int, significand: Decimal) {
#if FOUNDATION_FRAMEWORK
// Compatibility path
if Self.compatibility1 {
self.init(
_exponent: Int32(exponent) + significand._exponent,
_length: significand._length,
_isNegative: sign == .plus ? 0 : 1,
_isCompact: significand._isCompact,
_reserved: 0,
_mantissa: significand._mantissa
)
return
}
#endif

self = significand
do {
self = try significand._multiplyByPowerOfTen(
Expand Down
12 changes: 12 additions & 0 deletions Tests/FoundationEssentialsTests/DecimalTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1159,9 +1159,21 @@ final class DecimalTests : XCTestCase {
var x = -42 as Decimal
XCTAssertEqual(x.significand.sign, .plus)
var y = Decimal(sign: .plus, exponent: 0, significand: x)
#if FOUNDATION_FRAMEWORK
if Decimal.compatibility1 {
XCTAssertEqual(y, 42)
y = Decimal(sign: .minus, exponent: 0, significand: x)
XCTAssertEqual(y, -42)
} else {
XCTAssertEqual(y, -42)
y = Decimal(sign: .minus, exponent: 0, significand: x)
XCTAssertEqual(y, 42)
}
#else
XCTAssertEqual(y, -42)
y = Decimal(sign: .minus, exponent: 0, significand: x)
XCTAssertEqual(y, 42)
#endif

x = 42 as Decimal
XCTAssertEqual(x.significand.sign, .plus)
Expand Down