Skip to content

[SR-6671] Fix Decimal implementation of FloatingPoint constants #3067

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 1 commit into from
Aug 30, 2021
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
8 changes: 5 additions & 3 deletions Darwin/Foundation-swiftoverlay-Tests/TestDecimal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,15 @@ class TestDecimal : XCTestCase {
XCTAssertEqual(8, NSDecimalMaxSize)
XCTAssertEqual(32767, NSDecimalNoScale)
let smallest = Decimal(_exponent: 127, _length: 8, _isNegative: 1, _isCompact: 1, _reserved: 0, _mantissa: (UInt16.max, UInt16.max, UInt16.max, UInt16.max, UInt16.max, UInt16.max, UInt16.max, UInt16.max))
XCTAssertEqual(smallest, Decimal.leastFiniteMagnitude)
XCTAssertEqual(smallest, -Decimal.greatestFiniteMagnitude)
let biggest = Decimal(_exponent: 127, _length: 8, _isNegative: 0, _isCompact: 1, _reserved: 0, _mantissa: (UInt16.max, UInt16.max, UInt16.max, UInt16.max, UInt16.max, UInt16.max, UInt16.max, UInt16.max))
XCTAssertEqual(biggest, Decimal.greatestFiniteMagnitude)
let leastNormal = Decimal(_exponent: -127, _length: 1, _isNegative: 0, _isCompact: 1, _reserved: 0, _mantissa: (1, 0, 0, 0, 0, 0, 0, 0))
let leastNormal = Decimal(_exponent: -128, _length: 1, _isNegative: 0, _isCompact: 1, _reserved: 0, _mantissa: (1, 0, 0, 0, 0, 0, 0, 0))
XCTAssertEqual(leastNormal, Decimal.leastNormalMagnitude)
let leastNonzero = Decimal(_exponent: -127, _length: 1, _isNegative: 0, _isCompact: 1, _reserved: 0, _mantissa: (1, 0, 0, 0, 0, 0, 0, 0))
let leastNonzero = Decimal(_exponent: -128, _length: 1, _isNegative: 0, _isCompact: 1, _reserved: 0, _mantissa: (1, 0, 0, 0, 0, 0, 0, 0))
XCTAssertEqual(leastNonzero, Decimal.leastNonzeroMagnitude)
let leastFinite = 0 as Decimal
XCTAssertEqual(leastFinite, Decimal.leastFiniteMagnitude)
let pi = Decimal(_exponent: -38, _length: 8, _isNegative: 0, _isCompact: 1, _reserved: 0, _mantissa: (0x6623, 0x7d57, 0x16e7, 0xad0d, 0xaf52, 0x4641, 0xdfa7, 0xec58))
XCTAssertEqual(pi, Decimal.pi)
XCTAssertEqual(10, Decimal.radix)
Expand Down
22 changes: 4 additions & 18 deletions Darwin/Foundation-swiftoverlay/Decimal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -287,15 +287,6 @@ extension Decimal : Strideable {
// If it becomes clear that conformance is truly impossible, we can deprecate
// some of the methods (e.g. `isEqual(to:)` in favor of operators).
extension Decimal {
public static let leastFiniteMagnitude = Decimal(
_exponent: 127,
_length: 8,
_isNegative: 1,
_isCompact: 1,
_reserved: 0,
_mantissa: (0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff)
)

public static let greatestFiniteMagnitude = Decimal(
_exponent: 127,
_length: 8,
Expand All @@ -306,22 +297,17 @@ extension Decimal {
)

public static let leastNormalMagnitude = Decimal(
_exponent: -127,
_exponent: -128,
_length: 1,
_isNegative: 0,
_isCompact: 1,
_reserved: 0,
_mantissa: (0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000)
)

public static let leastNonzeroMagnitude = Decimal(
_exponent: -127,
_length: 1,
_isNegative: 0,
_isCompact: 1,
_reserved: 0,
_mantissa: (0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000)
)
public static let leastNonzeroMagnitude = leastNormalMagnitude

public static let leastFiniteMagnitude = zero

public static let pi = Decimal(
_exponent: -38,
Expand Down
22 changes: 4 additions & 18 deletions Sources/Foundation/Decimal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -495,15 +495,6 @@ extension Decimal : Strideable {
// If it becomes clear that conformance is truly impossible, we can deprecate
// some of the methods (e.g. `isEqual(to:)` in favor of operators).
extension Decimal {
public static let leastFiniteMagnitude = Decimal(
_exponent: 127,
_length: 8,
_isNegative: 1,
_isCompact: 1,
_reserved: 0,
_mantissa: (0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff)
)

public static let greatestFiniteMagnitude = Decimal(
_exponent: 127,
_length: 8,
Expand All @@ -514,22 +505,17 @@ extension Decimal {
)

public static let leastNormalMagnitude = Decimal(
_exponent: -127,
_exponent: -128,
_length: 1,
_isNegative: 0,
_isCompact: 1,
_reserved: 0,
_mantissa: (0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000)
)

public static let leastNonzeroMagnitude = Decimal(
_exponent: -127,
_length: 1,
_isNegative: 0,
_isCompact: 1,
_reserved: 0,
_mantissa: (0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000)
)
public static let leastNonzeroMagnitude = leastNormalMagnitude

public static let leastFiniteMagnitude = zero

public static let pi = Decimal(
_exponent: -38,
Expand Down
32 changes: 15 additions & 17 deletions Tests/Foundation/Tests/TestDecimal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,15 @@ class TestDecimal: XCTestCase {
XCTAssertEqual(8, NSDecimalMaxSize)
XCTAssertEqual(32767, NSDecimalNoScale)
let smallest = Decimal(_exponent: 127, _length: 8, _isNegative: 1, _isCompact: 1, _reserved: 0, _mantissa: (UInt16.max, UInt16.max, UInt16.max, UInt16.max, UInt16.max, UInt16.max, UInt16.max, UInt16.max))
XCTAssertEqual(smallest, Decimal.leastFiniteMagnitude)
XCTAssertEqual(smallest, -Decimal.greatestFiniteMagnitude)
let biggest = Decimal(_exponent: 127, _length: 8, _isNegative: 0, _isCompact: 1, _reserved: 0, _mantissa: (UInt16.max, UInt16.max, UInt16.max, UInt16.max, UInt16.max, UInt16.max, UInt16.max, UInt16.max))
XCTAssertEqual(biggest, Decimal.greatestFiniteMagnitude)
let leastNormal = Decimal(_exponent: -127, _length: 1, _isNegative: 0, _isCompact: 1, _reserved: 0, _mantissa: (1, 0, 0, 0, 0, 0, 0, 0))
let leastNormal = Decimal(_exponent: -128, _length: 1, _isNegative: 0, _isCompact: 1, _reserved: 0, _mantissa: (1, 0, 0, 0, 0, 0, 0, 0))
XCTAssertEqual(leastNormal, Decimal.leastNormalMagnitude)
let leastNonzero = Decimal(_exponent: -127, _length: 1, _isNegative: 0, _isCompact: 1, _reserved: 0, _mantissa: (1, 0, 0, 0, 0, 0, 0, 0))
let leastNonzero = Decimal(_exponent: -128, _length: 1, _isNegative: 0, _isCompact: 1, _reserved: 0, _mantissa: (1, 0, 0, 0, 0, 0, 0, 0))
XCTAssertEqual(leastNonzero, Decimal.leastNonzeroMagnitude)
let leastFinite = 0 as Decimal
XCTAssertEqual(leastFinite, Decimal.leastFiniteMagnitude)
let pi = Decimal(_exponent: -38, _length: 8, _isNegative: 0, _isCompact: 1, _reserved: 0, _mantissa: (0x6623, 0x7d57, 0x16e7, 0xad0d, 0xaf52, 0x4641, 0xdfa7, 0xec58))
XCTAssertEqual(pi, Decimal.pi)
XCTAssertEqual(10, Decimal.radix)
Expand Down Expand Up @@ -177,13 +179,12 @@ class TestDecimal: XCTestCase {
XCTAssertEqual("-5", Decimal(signOf: Decimal(-3), magnitudeOf: Decimal(-5)).description)
XCTAssertEqual("5", NSDecimalNumber(decimal: Decimal(5)).description)
XCTAssertEqual("-5", NSDecimalNumber(decimal: Decimal(-5)).description)
XCTAssertEqual("-3402823669209384634633746074317682114550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", Decimal.leastFiniteMagnitude.description)
XCTAssertEqual("3402823669209384634633746074317682114550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", Decimal.greatestFiniteMagnitude.description)
XCTAssertEqual("0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", Decimal.leastNormalMagnitude.description)
XCTAssertEqual("0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", Decimal.leastNonzeroMagnitude.description)
XCTAssertEqual("0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", Decimal.leastNormalMagnitude.description)
XCTAssertEqual("0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", Decimal.leastNonzeroMagnitude.description)
XCTAssertEqual("0", Decimal.leastFiniteMagnitude.description)

let fr = Locale(identifier: "fr_FR")
let leastFiniteMagnitude = "-3402823669209384634633746074317682114550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
let greatestFiniteMagnitude = "3402823669209384634633746074317682114550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"

XCTAssertEqual("0", NSDecimalNumber(decimal: Decimal()).description(withLocale: fr))
Expand All @@ -194,10 +195,9 @@ class TestDecimal: XCTestCase {
XCTAssertEqual("3,14159265358979323846264338327950288419", NSDecimalNumber(decimal: Decimal.pi).description(withLocale: fr))
XCTAssertEqual("-30000000000", NSDecimalNumber(decimal: Decimal(sign: .minus, exponent: 10, significand: Decimal(3))).description(withLocale: fr))
XCTAssertEqual("123456,789", NSDecimalNumber(decimal: Decimal(string: "123456.789")!).description(withLocale: fr))
XCTAssertEqual(leastFiniteMagnitude, NSDecimalNumber(decimal: Decimal.leastFiniteMagnitude).description(withLocale: fr))
XCTAssertEqual(greatestFiniteMagnitude, NSDecimalNumber(decimal: Decimal.greatestFiniteMagnitude).description(withLocale: fr))
XCTAssertEqual("0,0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", NSDecimalNumber(decimal: Decimal.leastNormalMagnitude).description(withLocale: fr))
XCTAssertEqual("0,0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", NSDecimalNumber(decimal: Decimal.leastNonzeroMagnitude).description(withLocale: fr))
XCTAssertEqual("0,00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", NSDecimalNumber(decimal: Decimal.leastNormalMagnitude).description(withLocale: fr))
XCTAssertEqual("0,00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", NSDecimalNumber(decimal: Decimal.leastNonzeroMagnitude).description(withLocale: fr))

let en = Locale(identifier: "en_GB")
XCTAssertEqual("0", NSDecimalNumber(decimal: Decimal()).description(withLocale: en))
Expand All @@ -208,10 +208,9 @@ class TestDecimal: XCTestCase {
XCTAssertEqual("3.14159265358979323846264338327950288419", NSDecimalNumber(decimal: Decimal.pi).description(withLocale: en))
XCTAssertEqual("-30000000000", NSDecimalNumber(decimal: Decimal(sign: .minus, exponent: 10, significand: Decimal(3))).description(withLocale: en))
XCTAssertEqual("123456.789", NSDecimalNumber(decimal: Decimal(string: "123456.789")!).description(withLocale: en))
XCTAssertEqual(leastFiniteMagnitude, NSDecimalNumber(decimal: Decimal.leastFiniteMagnitude).description(withLocale: en))
XCTAssertEqual(greatestFiniteMagnitude, NSDecimalNumber(decimal: Decimal.greatestFiniteMagnitude).description(withLocale: en))
XCTAssertEqual("0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", NSDecimalNumber(decimal: Decimal.leastNormalMagnitude).description(withLocale: en))
XCTAssertEqual("0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", NSDecimalNumber(decimal: Decimal.leastNonzeroMagnitude).description(withLocale: en))
XCTAssertEqual("0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", NSDecimalNumber(decimal: Decimal.leastNormalMagnitude).description(withLocale: en))
XCTAssertEqual("0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", NSDecimalNumber(decimal: Decimal.leastNonzeroMagnitude).description(withLocale: en))
}

func test_ExplicitConstruction() {
Expand Down Expand Up @@ -333,7 +332,6 @@ class TestDecimal: XCTestCase {
XCTAssertEqual(NSDecimalNumber(floatLiteral: 2880.4).subtracting(NSDecimalNumber(floatLiteral: 5538)), NSDecimalNumber(floatLiteral: 2880.4 - 5538))

XCTAssertEqual(Decimal.greatestFiniteMagnitude - Decimal.greatestFiniteMagnitude, Decimal(0))
XCTAssertEqual(Decimal.leastFiniteMagnitude - Decimal(1), Decimal.leastFiniteMagnitude)
let overflowed = Decimal.greatestFiniteMagnitude + Decimal.greatestFiniteMagnitude
XCTAssertTrue(overflowed.isNaN)

Expand Down Expand Up @@ -388,7 +386,7 @@ class TestDecimal: XCTestCase {
XCTAssertEqual((1 as Decimal).magnitude, abs(-1 as Decimal))
XCTAssertEqual((-1 as Decimal).magnitude, abs(-1 as Decimal))
XCTAssertEqual((-1 as Decimal).magnitude, abs(1 as Decimal))
XCTAssertEqual(Decimal.leastFiniteMagnitude.magnitude, -Decimal.leastFiniteMagnitude) // A bit of a misnomer.
XCTAssertEqual(Decimal.leastFiniteMagnitude.magnitude, -Decimal.leastFiniteMagnitude)
XCTAssertEqual(Decimal.greatestFiniteMagnitude.magnitude, Decimal.greatestFiniteMagnitude)
XCTAssertTrue(Decimal.nan.magnitude.isNaN)

Expand Down Expand Up @@ -568,15 +566,15 @@ class TestDecimal: XCTestCase {
XCTAssertTrue(Int(small.exponent) - Int(large.exponent) < Int(Int8.min))
XCTAssertNotEqual(small, large)

XCTAssertEqual(small.exponent, -127)
XCTAssertEqual(small.exponent, -128)
XCTAssertEqual(large.exponent, 127)
XCTAssertEqual(.lossOfPrecision, NSDecimalNormalize(&small, &large, .plain))
XCTAssertEqual(small.exponent, 127)
XCTAssertEqual(large.exponent, 127)

small = Decimal.leastNonzeroMagnitude
large = Decimal.greatestFiniteMagnitude
XCTAssertEqual(small.exponent, -127)
XCTAssertEqual(small.exponent, -128)
XCTAssertEqual(large.exponent, 127)
XCTAssertEqual(.lossOfPrecision, NSDecimalNormalize(&large, &small, .plain))
XCTAssertEqual(small.exponent, 127)
Expand Down
8 changes: 4 additions & 4 deletions Tests/Foundation/Tests/TestJSONSerialization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1428,10 +1428,10 @@ extension TestJSONSerialization {
}

func test_serialize_Decimal() {
XCTAssertEqual(try trySerialize([-Decimal.leastFiniteMagnitude]), "[3402823669209384634633746074317682114550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000]")
XCTAssertEqual(try trySerialize([Decimal.leastFiniteMagnitude]), "[-3402823669209384634633746074317682114550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000]")
XCTAssertEqual(try trySerialize([-Decimal.leastNonzeroMagnitude]), "[-0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001]")
XCTAssertEqual(try trySerialize([Decimal.leastNonzeroMagnitude]), "[0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001]")
XCTAssertEqual(try trySerialize([-Decimal.leastFiniteMagnitude]), "[0]")
XCTAssertEqual(try trySerialize([Decimal.leastFiniteMagnitude]), "[0]")
XCTAssertEqual(try trySerialize([-Decimal.leastNonzeroMagnitude]), "[-0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001]")
XCTAssertEqual(try trySerialize([Decimal.leastNonzeroMagnitude]), "[0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001]")
XCTAssertEqual(try trySerialize([-Decimal.greatestFiniteMagnitude]), "[-3402823669209384634633746074317682114550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000]")
XCTAssertEqual(try trySerialize([Decimal.greatestFiniteMagnitude]), "[3402823669209384634633746074317682114550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000]")
XCTAssertEqual(try trySerialize([Decimal(Int8.min), Decimal(Int8(0)), Decimal(Int8.max)]), "[-128,0,127]")
Expand Down