From cf00df297a046138b4b0d9226dfe3d02fda25c24 Mon Sep 17 00:00:00 2001 From: Xiaodi Wu <13952+xwu@users.noreply.github.com> Date: Sat, 24 Jul 2021 23:18:14 -0400 Subject: [PATCH] Partially fix Decimal.ulp to return a valid representation. --- Darwin/Foundation-swiftoverlay-Tests/TestDecimal.swift | 7 ++++++- Darwin/Foundation-swiftoverlay/Decimal.swift | 2 +- Sources/Foundation/Decimal.swift | 2 +- Tests/Foundation/Tests/TestDecimal.swift | 8 +++++++- 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/Darwin/Foundation-swiftoverlay-Tests/TestDecimal.swift b/Darwin/Foundation-swiftoverlay-Tests/TestDecimal.swift index 1ef1f86030..e933ee9b66 100644 --- a/Darwin/Foundation-swiftoverlay-Tests/TestDecimal.swift +++ b/Darwin/Foundation-swiftoverlay-Tests/TestDecimal.swift @@ -209,7 +209,7 @@ class TestDecimal : XCTestCase { let ulp = explicit.ulp XCTAssertEqual(0x7f, ulp.exponent) - XCTAssertEqual(8, ulp._length) + XCTAssertEqual(1, ulp._length) XCTAssertEqual(0, ulp._isNegative) XCTAssertEqual(1, ulp._isCompact) XCTAssertEqual(0, ulp._reserved) @@ -588,6 +588,11 @@ class TestDecimal : XCTestCase { } } } + + func test_ULP() { + let x = 0.1 as Decimal + XCTAssertFalse(x.ulp > x) + } func test_unconditionallyBridgeFromObjectiveC() { XCTAssertEqual(Decimal(), Decimal._unconditionallyBridgeFromObjectiveC(nil)) diff --git a/Darwin/Foundation-swiftoverlay/Decimal.swift b/Darwin/Foundation-swiftoverlay/Decimal.swift index 5a0854dc33..e98892f93d 100644 --- a/Darwin/Foundation-swiftoverlay/Decimal.swift +++ b/Darwin/Foundation-swiftoverlay/Decimal.swift @@ -503,7 +503,7 @@ extension Decimal { public var ulp: Decimal { if !self.isFinite { return Decimal.nan } return Decimal( - _exponent: _exponent, _length: 8, _isNegative: 0, _isCompact: 1, + _exponent: _exponent, _length: 1, _isNegative: 0, _isCompact: 1, _reserved: 0, _mantissa: (0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000)) } diff --git a/Sources/Foundation/Decimal.swift b/Sources/Foundation/Decimal.swift index 6d4d50fad4..3caffa645f 100644 --- a/Sources/Foundation/Decimal.swift +++ b/Sources/Foundation/Decimal.swift @@ -731,7 +731,7 @@ extension Decimal { public var ulp: Decimal { if !self.isFinite { return Decimal.nan } return Decimal( - _exponent: _exponent, _length: 8, _isNegative: 0, _isCompact: 1, + _exponent: _exponent, _length: 1, _isNegative: 0, _isCompact: 1, _reserved: 0, _mantissa: (0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000)) } diff --git a/Tests/Foundation/Tests/TestDecimal.swift b/Tests/Foundation/Tests/TestDecimal.swift index 590d7a99db..25ec9d39d5 100644 --- a/Tests/Foundation/Tests/TestDecimal.swift +++ b/Tests/Foundation/Tests/TestDecimal.swift @@ -271,7 +271,7 @@ class TestDecimal: XCTestCase { let ulp = explicit.ulp XCTAssertEqual(0x7f, ulp.exponent) - XCTAssertEqual(8, ulp._length) + XCTAssertEqual(1, ulp._length) XCTAssertEqual(0, ulp._isNegative) XCTAssertEqual(1, ulp._isCompact) XCTAssertEqual(0, ulp._reserved) @@ -824,6 +824,11 @@ class TestDecimal: XCTestCase { XCTAssertEqual(100,number.objCType.pointee, "ObjC type for NSDecimalNumber is 'd'") } + + func test_ULP() { + let x = 0.1 as Decimal + XCTAssertFalse(x.ulp > x) + } func test_ZeroPower() { let six = NSDecimalNumber(integerLiteral: 6) @@ -1430,6 +1435,7 @@ class TestDecimal: XCTestCase { ("test_ScanDecimal", test_ScanDecimal), ("test_SimpleMultiplication", test_SimpleMultiplication), ("test_SmallerNumbers", test_SmallerNumbers), + ("test_ULP", test_ULP), ("test_ZeroPower", test_ZeroPower), ("test_parseDouble", test_parseDouble), ("test_doubleValue", test_doubleValue),