Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix two Decimal members with respect to NaN #2377

Merged
merged 1 commit into from
Jun 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions Foundation/Decimal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -202,16 +202,12 @@ extension Decimal {
extension Decimal : Hashable, Comparable {
internal var doubleValue: Double {
if _length == 0 {
if _isNegative == 1 {
return Double.nan
} else {
return 0
}
return _isNegative == 1 ? Double.nan : 0
}

var d = 0.0
for idx in stride(from: min(_length, 8), to: 0, by: -1) {
d = d * 65536 + Double(self[idx - 1])
for idx in (0..<min(_length, 8)).reversed() {
d = d * 65536 + Double(self[idx])
}

if _exponent < 0 {
Expand Down Expand Up @@ -336,7 +332,11 @@ extension Decimal : ExpressibleByIntegerLiteral {

extension Decimal : SignedNumeric {
public var magnitude: Decimal {
return Decimal(_exponent: _exponent, _length: _length, _isNegative: 0, _isCompact: _isCompact, _reserved: 0, _mantissa: _mantissa)
guard _length != 0 else { return self }
return Decimal(
_exponent: self._exponent, _length: self._length,
_isNegative: 0, _isCompact: self._isCompact,
_reserved: 0, _mantissa: self._mantissa)
}

// FIXME(integers): implement properly
Expand Down
10 changes: 10 additions & 0 deletions TestFoundation/TestDecimal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,16 @@ class TestDecimal: XCTestCase {
XCTAssertEqual(Decimal(68040), Decimal(386).advanced(by: Decimal(67654)))
XCTAssertEqual(Decimal(1.234), abs(Decimal(1.234)))
XCTAssertEqual(Decimal(1.234), abs(Decimal(-1.234)))
XCTAssertEqual((0 as Decimal).magnitude, 0 as Decimal)
XCTAssertEqual((1 as Decimal).magnitude, 1 as Decimal)
XCTAssertEqual((1 as Decimal).magnitude, abs(1 as Decimal))
XCTAssertEqual((1 as Decimal).magnitude, abs(-1 as Decimal))
XCTAssertEqual((-1 as Decimal).magnitude, abs(-1 as Decimal))
XCTAssertEqual((-1 as Decimal).magnitude, abs(1 as Decimal))
XCTAssertEqual(Decimal.leastFiniteMagnitude.magnitude, -Decimal.leastFiniteMagnitude) // A bit of a misnomer.
XCTAssertEqual(Decimal.greatestFiniteMagnitude.magnitude, Decimal.greatestFiniteMagnitude)
XCTAssertTrue(Decimal.nan.magnitude.isNaN)

var a = Decimal(1234)
var result = Decimal(0)
XCTAssertEqual(.noError, NSDecimalMultiplyByPowerOf10(&result, &a, 1, .plain))
Expand Down