Skip to content

Commit 0aa7855

Browse files
committed
[SR-6671] Fix Decimal implementation of FloatingPoint constants.
1 parent bb8e7d2 commit 0aa7855

File tree

5 files changed

+35
-58
lines changed

5 files changed

+35
-58
lines changed

Darwin/Foundation-swiftoverlay-Tests/TestDecimal.swift

+5-3
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,15 @@ class TestDecimal : XCTestCase {
116116
XCTAssertEqual(8, NSDecimalMaxSize)
117117
XCTAssertEqual(32767, NSDecimalNoScale)
118118
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))
119-
XCTAssertEqual(smallest, Decimal.leastFiniteMagnitude)
119+
XCTAssertEqual(smallest, -Decimal.greatestFiniteMagnitude)
120120
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))
121121
XCTAssertEqual(biggest, Decimal.greatestFiniteMagnitude)
122-
let leastNormal = Decimal(_exponent: -127, _length: 1, _isNegative: 0, _isCompact: 1, _reserved: 0, _mantissa: (1, 0, 0, 0, 0, 0, 0, 0))
122+
let leastNormal = Decimal(_exponent: -128, _length: 1, _isNegative: 0, _isCompact: 1, _reserved: 0, _mantissa: (1, 0, 0, 0, 0, 0, 0, 0))
123123
XCTAssertEqual(leastNormal, Decimal.leastNormalMagnitude)
124-
let leastNonzero = Decimal(_exponent: -127, _length: 1, _isNegative: 0, _isCompact: 1, _reserved: 0, _mantissa: (1, 0, 0, 0, 0, 0, 0, 0))
124+
let leastNonzero = Decimal(_exponent: -128, _length: 1, _isNegative: 0, _isCompact: 1, _reserved: 0, _mantissa: (1, 0, 0, 0, 0, 0, 0, 0))
125125
XCTAssertEqual(leastNonzero, Decimal.leastNonzeroMagnitude)
126+
let leastFinite = 0 as Decimal
127+
XCTAssertEqual(leastFinite, Decimal.leastFiniteMagnitude)
126128
let pi = Decimal(_exponent: -38, _length: 8, _isNegative: 0, _isCompact: 1, _reserved: 0, _mantissa: (0x6623, 0x7d57, 0x16e7, 0xad0d, 0xaf52, 0x4641, 0xdfa7, 0xec58))
127129
XCTAssertEqual(pi, Decimal.pi)
128130
XCTAssertEqual(10, Decimal.radix)

Darwin/Foundation-swiftoverlay/Decimal.swift

+4-18
Original file line numberDiff line numberDiff line change
@@ -287,15 +287,6 @@ extension Decimal : Strideable {
287287
// If it becomes clear that conformance is truly impossible, we can deprecate
288288
// some of the methods (e.g. `isEqual(to:)` in favor of operators).
289289
extension Decimal {
290-
public static let leastFiniteMagnitude = Decimal(
291-
_exponent: 127,
292-
_length: 8,
293-
_isNegative: 1,
294-
_isCompact: 1,
295-
_reserved: 0,
296-
_mantissa: (0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff)
297-
)
298-
299290
public static let greatestFiniteMagnitude = Decimal(
300291
_exponent: 127,
301292
_length: 8,
@@ -306,22 +297,17 @@ extension Decimal {
306297
)
307298

308299
public static let leastNormalMagnitude = Decimal(
309-
_exponent: -127,
300+
_exponent: -128,
310301
_length: 1,
311302
_isNegative: 0,
312303
_isCompact: 1,
313304
_reserved: 0,
314305
_mantissa: (0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000)
315306
)
316307

317-
public static let leastNonzeroMagnitude = Decimal(
318-
_exponent: -127,
319-
_length: 1,
320-
_isNegative: 0,
321-
_isCompact: 1,
322-
_reserved: 0,
323-
_mantissa: (0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000)
324-
)
308+
public static let leastNonzeroMagnitude = leastNormalMagnitude
309+
310+
public static let leastFiniteMagnitude = zero
325311

326312
public static let pi = Decimal(
327313
_exponent: -38,

Sources/Foundation/Decimal.swift

+4-18
Original file line numberDiff line numberDiff line change
@@ -495,15 +495,6 @@ extension Decimal : Strideable {
495495
// If it becomes clear that conformance is truly impossible, we can deprecate
496496
// some of the methods (e.g. `isEqual(to:)` in favor of operators).
497497
extension Decimal {
498-
public static let leastFiniteMagnitude = Decimal(
499-
_exponent: 127,
500-
_length: 8,
501-
_isNegative: 1,
502-
_isCompact: 1,
503-
_reserved: 0,
504-
_mantissa: (0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff)
505-
)
506-
507498
public static let greatestFiniteMagnitude = Decimal(
508499
_exponent: 127,
509500
_length: 8,
@@ -514,22 +505,17 @@ extension Decimal {
514505
)
515506

516507
public static let leastNormalMagnitude = Decimal(
517-
_exponent: -127,
508+
_exponent: -128,
518509
_length: 1,
519510
_isNegative: 0,
520511
_isCompact: 1,
521512
_reserved: 0,
522513
_mantissa: (0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000)
523514
)
524515

525-
public static let leastNonzeroMagnitude = Decimal(
526-
_exponent: -127,
527-
_length: 1,
528-
_isNegative: 0,
529-
_isCompact: 1,
530-
_reserved: 0,
531-
_mantissa: (0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000)
532-
)
516+
public static let leastNonzeroMagnitude = leastNormalMagnitude
517+
518+
public static let leastFiniteMagnitude = zero
533519

534520
public static let pi = Decimal(
535521
_exponent: -38,

Tests/Foundation/Tests/TestDecimal.swift

+18-15
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
88
//
99

10+
import Foundation
11+
1012
class TestDecimal: XCTestCase {
1113

1214
func test_NSDecimalNumberInit() {
@@ -137,13 +139,15 @@ class TestDecimal: XCTestCase {
137139
XCTAssertEqual(8, NSDecimalMaxSize)
138140
XCTAssertEqual(32767, NSDecimalNoScale)
139141
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))
140-
XCTAssertEqual(smallest, Decimal.leastFiniteMagnitude)
142+
XCTAssertEqual(smallest, -Decimal.greatestFiniteMagnitude)
141143
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))
142144
XCTAssertEqual(biggest, Decimal.greatestFiniteMagnitude)
143-
let leastNormal = Decimal(_exponent: -127, _length: 1, _isNegative: 0, _isCompact: 1, _reserved: 0, _mantissa: (1, 0, 0, 0, 0, 0, 0, 0))
145+
let leastNormal = Decimal(_exponent: -128, _length: 1, _isNegative: 0, _isCompact: 1, _reserved: 0, _mantissa: (1, 0, 0, 0, 0, 0, 0, 0))
144146
XCTAssertEqual(leastNormal, Decimal.leastNormalMagnitude)
145-
let leastNonzero = Decimal(_exponent: -127, _length: 1, _isNegative: 0, _isCompact: 1, _reserved: 0, _mantissa: (1, 0, 0, 0, 0, 0, 0, 0))
147+
let leastNonzero = Decimal(_exponent: -128, _length: 1, _isNegative: 0, _isCompact: 1, _reserved: 0, _mantissa: (1, 0, 0, 0, 0, 0, 0, 0))
146148
XCTAssertEqual(leastNonzero, Decimal.leastNonzeroMagnitude)
149+
let leastFinite = 0 as Decimal
150+
XCTAssertEqual(leastFinite, Decimal.leastFiniteMagnitude)
147151
let pi = Decimal(_exponent: -38, _length: 8, _isNegative: 0, _isCompact: 1, _reserved: 0, _mantissa: (0x6623, 0x7d57, 0x16e7, 0xad0d, 0xaf52, 0x4641, 0xdfa7, 0xec58))
148152
XCTAssertEqual(pi, Decimal.pi)
149153
XCTAssertEqual(10, Decimal.radix)
@@ -177,10 +181,10 @@ class TestDecimal: XCTestCase {
177181
XCTAssertEqual("-5", Decimal(signOf: Decimal(-3), magnitudeOf: Decimal(-5)).description)
178182
XCTAssertEqual("5", NSDecimalNumber(decimal: Decimal(5)).description)
179183
XCTAssertEqual("-5", NSDecimalNumber(decimal: Decimal(-5)).description)
180-
XCTAssertEqual("-3402823669209384634633746074317682114550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", Decimal.leastFiniteMagnitude.description)
181184
XCTAssertEqual("3402823669209384634633746074317682114550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", Decimal.greatestFiniteMagnitude.description)
182-
XCTAssertEqual("0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", Decimal.leastNormalMagnitude.description)
183-
XCTAssertEqual("0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", Decimal.leastNonzeroMagnitude.description)
185+
XCTAssertEqual("0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", Decimal.leastNormalMagnitude.description)
186+
XCTAssertEqual("0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", Decimal.leastNonzeroMagnitude.description)
187+
XCTAssertEqual("0", Decimal.leastFiniteMagnitude.description)
184188

185189
let fr = Locale(identifier: "fr_FR")
186190
let leastFiniteMagnitude = "-3402823669209384634633746074317682114550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
@@ -194,10 +198,10 @@ class TestDecimal: XCTestCase {
194198
XCTAssertEqual("3,14159265358979323846264338327950288419", NSDecimalNumber(decimal: Decimal.pi).description(withLocale: fr))
195199
XCTAssertEqual("-30000000000", NSDecimalNumber(decimal: Decimal(sign: .minus, exponent: 10, significand: Decimal(3))).description(withLocale: fr))
196200
XCTAssertEqual("123456,789", NSDecimalNumber(decimal: Decimal(string: "123456.789")!).description(withLocale: fr))
197-
XCTAssertEqual(leastFiniteMagnitude, NSDecimalNumber(decimal: Decimal.leastFiniteMagnitude).description(withLocale: fr))
198201
XCTAssertEqual(greatestFiniteMagnitude, NSDecimalNumber(decimal: Decimal.greatestFiniteMagnitude).description(withLocale: fr))
199-
XCTAssertEqual("0,0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", NSDecimalNumber(decimal: Decimal.leastNormalMagnitude).description(withLocale: fr))
200-
XCTAssertEqual("0,0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", NSDecimalNumber(decimal: Decimal.leastNonzeroMagnitude).description(withLocale: fr))
202+
XCTAssertEqual("0,00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", NSDecimalNumber(decimal: Decimal.leastNormalMagnitude).description(withLocale: fr))
203+
XCTAssertEqual("0,00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", NSDecimalNumber(decimal: Decimal.leastNonzeroMagnitude).description(withLocale: fr))
204+
XCTAssertEqual(leastFiniteMagnitude, NSDecimalNumber(decimal: Decimal.leastFiniteMagnitude).description(withLocale: fr))
201205

202206
let en = Locale(identifier: "en_GB")
203207
XCTAssertEqual("0", NSDecimalNumber(decimal: Decimal()).description(withLocale: en))
@@ -208,10 +212,10 @@ class TestDecimal: XCTestCase {
208212
XCTAssertEqual("3.14159265358979323846264338327950288419", NSDecimalNumber(decimal: Decimal.pi).description(withLocale: en))
209213
XCTAssertEqual("-30000000000", NSDecimalNumber(decimal: Decimal(sign: .minus, exponent: 10, significand: Decimal(3))).description(withLocale: en))
210214
XCTAssertEqual("123456.789", NSDecimalNumber(decimal: Decimal(string: "123456.789")!).description(withLocale: en))
211-
XCTAssertEqual(leastFiniteMagnitude, NSDecimalNumber(decimal: Decimal.leastFiniteMagnitude).description(withLocale: en))
212215
XCTAssertEqual(greatestFiniteMagnitude, NSDecimalNumber(decimal: Decimal.greatestFiniteMagnitude).description(withLocale: en))
213-
XCTAssertEqual("0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", NSDecimalNumber(decimal: Decimal.leastNormalMagnitude).description(withLocale: en))
216+
XCTAssertEqual("0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", NSDecimalNumber(decimal: Decimal.leastNormalMagnitude).description(withLocale: en))
214217
XCTAssertEqual("0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", NSDecimalNumber(decimal: Decimal.leastNonzeroMagnitude).description(withLocale: en))
218+
XCTAssertEqual(leastFiniteMagnitude, NSDecimalNumber(decimal: Decimal.leastFiniteMagnitude).description(withLocale: en))
215219
}
216220

217221
func test_ExplicitConstruction() {
@@ -333,7 +337,6 @@ class TestDecimal: XCTestCase {
333337
XCTAssertEqual(NSDecimalNumber(floatLiteral: 2880.4).subtracting(NSDecimalNumber(floatLiteral: 5538)), NSDecimalNumber(floatLiteral: 2880.4 - 5538))
334338

335339
XCTAssertEqual(Decimal.greatestFiniteMagnitude - Decimal.greatestFiniteMagnitude, Decimal(0))
336-
XCTAssertEqual(Decimal.leastFiniteMagnitude - Decimal(1), Decimal.leastFiniteMagnitude)
337340
let overflowed = Decimal.greatestFiniteMagnitude + Decimal.greatestFiniteMagnitude
338341
XCTAssertTrue(overflowed.isNaN)
339342

@@ -388,7 +391,7 @@ class TestDecimal: XCTestCase {
388391
XCTAssertEqual((1 as Decimal).magnitude, abs(-1 as Decimal))
389392
XCTAssertEqual((-1 as Decimal).magnitude, abs(-1 as Decimal))
390393
XCTAssertEqual((-1 as Decimal).magnitude, abs(1 as Decimal))
391-
XCTAssertEqual(Decimal.leastFiniteMagnitude.magnitude, -Decimal.leastFiniteMagnitude) // A bit of a misnomer.
394+
XCTAssertEqual(Decimal.leastFiniteMagnitude.magnitude, -Decimal.leastFiniteMagnitude)
392395
XCTAssertEqual(Decimal.greatestFiniteMagnitude.magnitude, Decimal.greatestFiniteMagnitude)
393396
XCTAssertTrue(Decimal.nan.magnitude.isNaN)
394397

@@ -568,15 +571,15 @@ class TestDecimal: XCTestCase {
568571
XCTAssertTrue(Int(small.exponent) - Int(large.exponent) < Int(Int8.min))
569572
XCTAssertNotEqual(small, large)
570573

571-
XCTAssertEqual(small.exponent, -127)
574+
XCTAssertEqual(small.exponent, -128)
572575
XCTAssertEqual(large.exponent, 127)
573576
XCTAssertEqual(.lossOfPrecision, NSDecimalNormalize(&small, &large, .plain))
574577
XCTAssertEqual(small.exponent, 127)
575578
XCTAssertEqual(large.exponent, 127)
576579

577580
small = Decimal.leastNonzeroMagnitude
578581
large = Decimal.greatestFiniteMagnitude
579-
XCTAssertEqual(small.exponent, -127)
582+
XCTAssertEqual(small.exponent, -128)
580583
XCTAssertEqual(large.exponent, 127)
581584
XCTAssertEqual(.lossOfPrecision, NSDecimalNormalize(&large, &small, .plain))
582585
XCTAssertEqual(small.exponent, 127)

Tests/Foundation/Tests/TestJSONSerialization.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -1428,10 +1428,10 @@ extension TestJSONSerialization {
14281428
}
14291429

14301430
func test_serialize_Decimal() {
1431-
XCTAssertEqual(try trySerialize([-Decimal.leastFiniteMagnitude]), "[3402823669209384634633746074317682114550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000]")
1432-
XCTAssertEqual(try trySerialize([Decimal.leastFiniteMagnitude]), "[-3402823669209384634633746074317682114550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000]")
1433-
XCTAssertEqual(try trySerialize([-Decimal.leastNonzeroMagnitude]), "[-0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001]")
1434-
XCTAssertEqual(try trySerialize([Decimal.leastNonzeroMagnitude]), "[0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001]")
1431+
XCTAssertEqual(try trySerialize([-Decimal.leastFiniteMagnitude]), "[0]")
1432+
XCTAssertEqual(try trySerialize([Decimal.leastFiniteMagnitude]), "[0]")
1433+
XCTAssertEqual(try trySerialize([-Decimal.leastNonzeroMagnitude]), "[-0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001]")
1434+
XCTAssertEqual(try trySerialize([Decimal.leastNonzeroMagnitude]), "[0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001]")
14351435
XCTAssertEqual(try trySerialize([-Decimal.greatestFiniteMagnitude]), "[-3402823669209384634633746074317682114550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000]")
14361436
XCTAssertEqual(try trySerialize([Decimal.greatestFiniteMagnitude]), "[3402823669209384634633746074317682114550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000]")
14371437
XCTAssertEqual(try trySerialize([Decimal(Int8.min), Decimal(Int8(0)), Decimal(Int8.max)]), "[-128,0,127]")

0 commit comments

Comments
 (0)