From cd32f345ec26159bf82363709bfc499bae817538 Mon Sep 17 00:00:00 2001 From: Xiaodi Wu Date: Fri, 26 Aug 2016 03:54:28 -0500 Subject: [PATCH 1/2] Fix Decimal --- stdlib/public/SDK/Foundation/Decimal.swift | 7 ++++--- test/Interpreter/SDK/NSDecimal.swift | 7 +++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/stdlib/public/SDK/Foundation/Decimal.swift b/stdlib/public/SDK/Foundation/Decimal.swift index 413089fbde663..41e663d00f458 100644 --- a/stdlib/public/SDK/Foundation/Decimal.swift +++ b/stdlib/public/SDK/Foundation/Decimal.swift @@ -31,12 +31,12 @@ extension Decimal { public var significand: Decimal { get { - return Decimal(_exponent: 1, _length: _length, _isNegative: _isNegative, _isCompact: _isCompact, _reserved: 0, _mantissa: _mantissa) + return Decimal(_exponent: 0, _length: _length, _isNegative: _isNegative, _isCompact: _isCompact, _reserved: 0, _mantissa: _mantissa) } } public init(sign: FloatingPointSign, exponent: Int, significand: Decimal) { - self.init(_exponent: Int32(exponent), _length: significand._length, _isNegative: sign == .plus ? 1 : 0, _isCompact: significand._isCompact, _reserved: 0, _mantissa: significand._mantissa) + self.init(_exponent: Int32(exponent) + significand._exponent, _length: significand._length, _isNegative: sign == .plus ? 0 : 1, _isCompact: significand._isCompact, _reserved: 0, _mantissa: significand._mantissa) } public init(signOf: Decimal, magnitudeOf magnitude: Decimal) { @@ -50,7 +50,8 @@ extension Decimal { public static var radix: Int { return 10 } public var ulp: Decimal { - return Decimal(_exponent: 1, _length: _length, _isNegative: _isNegative, _isCompact: _isCompact, _reserved: 0, _mantissa: _mantissa) + if !self.isFinite { return Decimal.nan } + return Decimal(_exponent: _exponent, _length: 8, _isNegative: 0, _isCompact: 1, _reserved: 0, _mantissa: (0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000)) } @available(*, unavailable, message: "Decimal does not yet fully adopt FloatingPoint.") diff --git a/test/Interpreter/SDK/NSDecimal.swift b/test/Interpreter/SDK/NSDecimal.swift index f14530c3a9432..72bc2baefc343 100644 --- a/test/Interpreter/SDK/NSDecimal.swift +++ b/test/Interpreter/SDK/NSDecimal.swift @@ -117,3 +117,10 @@ twenty = Decimal(20) ten = Decimal(10) twenty.divide(by: ten) print(twenty) // CHECK: 2 + +twenty = Decimal(20) +print(twenty.significand) // CHECK: 2 +print(twenty.exponent) // CHECK: 1 +print(twenty.ulp) // CHECK: 10 + +print(Decimal(sign: .plus, exponent: -2, significand: 100)) // CHECK: 1 From cb1df2f8904ac22b10663439e67703828f6b1400 Mon Sep 17 00:00:00 2001 From: Xiaodi Wu Date: Fri, 26 Aug 2016 12:23:44 -0500 Subject: [PATCH 2/2] Change test to address reviewer comments --- test/Interpreter/SDK/NSDecimal.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Interpreter/SDK/NSDecimal.swift b/test/Interpreter/SDK/NSDecimal.swift index 72bc2baefc343..cf111b9476e16 100644 --- a/test/Interpreter/SDK/NSDecimal.swift +++ b/test/Interpreter/SDK/NSDecimal.swift @@ -118,7 +118,7 @@ ten = Decimal(10) twenty.divide(by: ten) print(twenty) // CHECK: 2 -twenty = Decimal(20) +twenty = NSDecimalNumber(mantissa: 2, exponent: 1, isNegative: false) as Decimal print(twenty.significand) // CHECK: 2 print(twenty.exponent) // CHECK: 1 print(twenty.ulp) // CHECK: 10