-
Notifications
You must be signed in to change notification settings - Fork 1.2k
SR-7650: Incorrect result adding and subtracting Decimals #1550
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -124,6 +124,10 @@ class TestDecimal: XCTestCase { | |
XCTAssertFalse(zero.isInfinite) | ||
XCTAssertFalse(zero.isNaN) | ||
XCTAssertFalse(zero.isSignaling) | ||
|
||
let d1 = Decimal(1234567890123456789 as UInt64) | ||
XCTAssertEqual(d1._exponent, 0) | ||
XCTAssertEqual(d1._length, 4) | ||
} | ||
func test_Constants() { | ||
XCTAssertEqual(8, NSDecimalMaxSize) | ||
|
@@ -273,7 +277,31 @@ class TestDecimal: XCTestCase { | |
} | ||
} | ||
} | ||
|
||
XCTAssertEqual(Decimal(186243 * 15673 as Int64), Decimal(186243) * Decimal(15673)) | ||
|
||
XCTAssertEqual(Decimal(string: "5538")! + Decimal(string: "2880.4")!, Decimal(string: "8418.4")!) | ||
XCTAssertEqual(NSDecimalNumber(floatLiteral: 5538).adding(NSDecimalNumber(floatLiteral: 2880.4)), NSDecimalNumber(floatLiteral: 5538 + 2880.4)) | ||
|
||
XCTAssertEqual(Decimal(string: "5538.0")! - Decimal(string: "2880.4")!, Decimal(string: "2657.6")!) | ||
XCTAssertEqual(Decimal(string: "2880.4")! - Decimal(5538), Decimal(string: "-2657.6")!) | ||
XCTAssertEqual(Decimal(0x10000) - Decimal(0x1000), Decimal(0xf000)) | ||
XCTAssertEqual(Decimal(0x1_0000_0000) - Decimal(0x1000), Decimal(0xFFFFF000)) | ||
XCTAssertEqual(Decimal(0x1_0000_0000_0000) - Decimal(0x1000), Decimal(0xFFFFFFFFF000)) | ||
XCTAssertEqual(Decimal(1234_5678_9012_3456_7899 as UInt64) - Decimal(1234_5678_9012_3456_7890 as UInt64), Decimal(9)) | ||
XCTAssertEqual(Decimal(0xffdd_bb00_8866_4422 as UInt64) - Decimal(0x7777_7777), Decimal(0xFFDD_BB00_10EE_CCAB as UInt64)) | ||
XCTAssertEqual(NSDecimalNumber(floatLiteral: 5538).subtracting(NSDecimalNumber(floatLiteral: 2880.4)), NSDecimalNumber(floatLiteral: 5538 - 2880.4)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I’d rather these be initialized from strings to avoid floating-point representation shenanigans. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where ‘these’ are all Decimal/NSDecimalNumber instances created from floating-point values. |
||
XCTAssertEqual(NSDecimalNumber(floatLiteral: 2880.4).subtracting(NSDecimalNumber(floatLiteral: 5538)), NSDecimalNumber(floatLiteral: 2880.4 - 5538)) | ||
|
||
XCTAssertEqual(Decimal.greatestFiniteMagnitude - Decimal.greatestFiniteMagnitude, Decimal(0)) | ||
XCTAssertEqual(Decimal.leastFiniteMagnitude - Decimal(1), Decimal.leastFiniteMagnitude) | ||
let overflowed = Decimal.greatestFiniteMagnitude + Decimal.greatestFiniteMagnitude | ||
XCTAssertTrue(overflowed.isNaN) | ||
|
||
let highBit = Decimal(_exponent: 0, _length: 8, _isNegative: 0, _isCompact: 1, _reserved: 0, _mantissa: (0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x8000)) | ||
let otherBits = Decimal(_exponent: 0, _length: 8, _isNegative: 0, _isCompact: 1, _reserved: 0, _mantissa: (0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0x7fff)) | ||
XCTAssertEqual(highBit - otherBits, Decimal(1)) | ||
XCTAssertEqual(otherBits + Decimal(1), highBit) | ||
} | ||
|
||
func test_Misc() { | ||
|
@@ -478,6 +506,10 @@ class TestDecimal: XCTestCase { | |
XCTAssertEqual(1, f._isCompact) | ||
let after = f.description | ||
XCTAssertEqual(before, after) | ||
|
||
let nsd1 = NSDecimalNumber(decimal: Decimal(2657.6)) | ||
let nsd2 = NSDecimalNumber(floatLiteral: 2657.6) | ||
XCTAssertEqual(nsd1, nsd2) | ||
} | ||
|
||
func test_PositivePowers() { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a really over-complicated way to do this instead of using the wrapping operators, but it's correct, so it's OK for now.