Skip to content

Commit 774d396

Browse files
authored
Merge pull request #3014 from xwu/decimal-ulp-length
Partially fix Decimal.ulp to return a valid representation.
2 parents 0e4e031 + 8a020be commit 774d396

File tree

4 files changed

+15
-4
lines changed

4 files changed

+15
-4
lines changed

Darwin/Foundation-swiftoverlay-Tests/TestDecimal.swift

+6-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ class TestDecimal : XCTestCase {
209209

210210
let ulp = explicit.ulp
211211
XCTAssertEqual(0x7f, ulp.exponent)
212-
XCTAssertEqual(8, ulp._length)
212+
XCTAssertEqual(1, ulp._length)
213213
XCTAssertEqual(0, ulp._isNegative)
214214
XCTAssertEqual(1, ulp._isCompact)
215215
XCTAssertEqual(0, ulp._reserved)
@@ -588,6 +588,11 @@ class TestDecimal : XCTestCase {
588588
}
589589
}
590590
}
591+
592+
func test_ULP() {
593+
let x = 0.1 as Decimal
594+
XCTAssertFalse(x.ulp > x)
595+
}
591596

592597
func test_unconditionallyBridgeFromObjectiveC() {
593598
XCTAssertEqual(Decimal(), Decimal._unconditionallyBridgeFromObjectiveC(nil))

Darwin/Foundation-swiftoverlay/Decimal.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ extension Decimal {
503503
public var ulp: Decimal {
504504
if !self.isFinite { return Decimal.nan }
505505
return Decimal(
506-
_exponent: _exponent, _length: 8, _isNegative: 0, _isCompact: 1,
506+
_exponent: _exponent, _length: 1, _isNegative: 0, _isCompact: 1,
507507
_reserved: 0, _mantissa: (0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000))
508508
}
509509

Sources/Foundation/Decimal.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@ extension Decimal {
731731
public var ulp: Decimal {
732732
if !self.isFinite { return Decimal.nan }
733733
return Decimal(
734-
_exponent: _exponent, _length: 8, _isNegative: 0, _isCompact: 1,
734+
_exponent: _exponent, _length: 1, _isNegative: 0, _isCompact: 1,
735735
_reserved: 0, _mantissa: (0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000))
736736
}
737737

Tests/Foundation/Tests/TestDecimal.swift

+7-1
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ class TestDecimal: XCTestCase {
271271

272272
let ulp = explicit.ulp
273273
XCTAssertEqual(0x7f, ulp.exponent)
274-
XCTAssertEqual(8, ulp._length)
274+
XCTAssertEqual(1, ulp._length)
275275
XCTAssertEqual(0, ulp._isNegative)
276276
XCTAssertEqual(1, ulp._isCompact)
277277
XCTAssertEqual(0, ulp._reserved)
@@ -851,6 +851,11 @@ class TestDecimal: XCTestCase {
851851

852852
XCTAssertEqual(100,number.objCType.pointee, "ObjC type for NSDecimalNumber is 'd'")
853853
}
854+
855+
func test_ULP() {
856+
let x = 0.1 as Decimal
857+
XCTAssertFalse(x.ulp > x)
858+
}
854859

855860
func test_ZeroPower() {
856861
let six = NSDecimalNumber(integerLiteral: 6)
@@ -1457,6 +1462,7 @@ class TestDecimal: XCTestCase {
14571462
("test_ScanDecimal", test_ScanDecimal),
14581463
("test_SimpleMultiplication", test_SimpleMultiplication),
14591464
("test_SmallerNumbers", test_SmallerNumbers),
1465+
("test_ULP", test_ULP),
14601466
("test_ZeroPower", test_ZeroPower),
14611467
("test_parseDouble", test_parseDouble),
14621468
("test_doubleValue", test_doubleValue),

0 commit comments

Comments
 (0)