Skip to content

Commit 60ce7f5

Browse files
authored
Merge pull request swiftlang#4517 from xwu/fix-decimal
2 parents 3d02f52 + cb1df2f commit 60ce7f5

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

stdlib/public/SDK/Foundation/Decimal.swift

+4-3
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ extension Decimal {
3131

3232
public var significand: Decimal {
3333
get {
34-
return Decimal(_exponent: 1, _length: _length, _isNegative: _isNegative, _isCompact: _isCompact, _reserved: 0, _mantissa: _mantissa)
34+
return Decimal(_exponent: 0, _length: _length, _isNegative: _isNegative, _isCompact: _isCompact, _reserved: 0, _mantissa: _mantissa)
3535
}
3636
}
3737

3838
public init(sign: FloatingPointSign, exponent: Int, significand: Decimal) {
39-
self.init(_exponent: Int32(exponent), _length: significand._length, _isNegative: sign == .plus ? 1 : 0, _isCompact: significand._isCompact, _reserved: 0, _mantissa: significand._mantissa)
39+
self.init(_exponent: Int32(exponent) + significand._exponent, _length: significand._length, _isNegative: sign == .plus ? 0 : 1, _isCompact: significand._isCompact, _reserved: 0, _mantissa: significand._mantissa)
4040
}
4141

4242
public init(signOf: Decimal, magnitudeOf magnitude: Decimal) {
@@ -50,7 +50,8 @@ extension Decimal {
5050
public static var radix: Int { return 10 }
5151

5252
public var ulp: Decimal {
53-
return Decimal(_exponent: 1, _length: _length, _isNegative: _isNegative, _isCompact: _isCompact, _reserved: 0, _mantissa: _mantissa)
53+
if !self.isFinite { return Decimal.nan }
54+
return Decimal(_exponent: _exponent, _length: 8, _isNegative: 0, _isCompact: 1, _reserved: 0, _mantissa: (0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000))
5455
}
5556

5657
@available(*, unavailable, message: "Decimal does not yet fully adopt FloatingPoint.")

test/Interpreter/SDK/NSDecimal.swift

+7
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,10 @@ twenty = Decimal(20)
117117
ten = Decimal(10)
118118
twenty.divide(by: ten)
119119
print(twenty) // CHECK: 2
120+
121+
twenty = NSDecimalNumber(mantissa: 2, exponent: 1, isNegative: false) as Decimal
122+
print(twenty.significand) // CHECK: 2
123+
print(twenty.exponent) // CHECK: 1
124+
print(twenty.ulp) // CHECK: 10
125+
126+
print(Decimal(sign: .plus, exponent: -2, significand: 100)) // CHECK: 1

0 commit comments

Comments
 (0)