-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[SR-13015] Decimal calculation incorrect on Linux #3982
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
Comments
This can be simplified down to print(Decimal(string: "498.7509045")! + Decimal(string: "8.453441368210501065891847765109162027")! == Decimal(string: "507.2043458682105010658918477651091")!) Which gives |
Comment by Matt Galloway (JIRA) @spevans thanks for that! Yes you're right. And if you remove the |
Comment by Matt Galloway (JIRA) I've narrowed down to even this: Decimal(string: "429.5000001")! + Decimal(string: "0.000000000000000000000000000000000001")! == Decimal(string: "429.500000100000000000000000000000000001")! That's |
Comment by Matt Galloway (JIRA) I think it's in here: At that point, I'll keep going down this rabbit hole. |
Yeah It looks to be func test_13015() {
let a = Decimal(string: "498.7509045")!
let b = Decimal(string: "8.453441368210501065891847765109162027")!
var aNormalized = a
var bNormalized = b
let normalizeError = NSDecimalNormalize(&aNormalized, &bNormalized, .plain)
XCTAssertEqual(normalizeError, NSDecimalNumber.CalculationError.lossOfPrecision)
XCTAssertEqual(aNormalized.exponent, -31)
XCTAssertEqual(aNormalized._mantissa.0, 0)
XCTAssertEqual(aNormalized._mantissa.1, 21760)
XCTAssertEqual(aNormalized._mantissa.2, 45355)
XCTAssertEqual(aNormalized._mantissa.3, 11455)
XCTAssertEqual(aNormalized._mantissa.4, 62709)
XCTAssertEqual(aNormalized._mantissa.5, 14050)
XCTAssertEqual(aNormalized._mantissa.6, 62951)
XCTAssertEqual(aNormalized._mantissa.7, 0)
XCTAssertEqual(bNormalized.exponent, -31)
XCTAssertEqual(bNormalized._mantissa.0, 56467)
XCTAssertEqual(bNormalized._mantissa.1, 17616)
XCTAssertEqual(bNormalized._mantissa.2, 59987)
XCTAssertEqual(bNormalized._mantissa.3, 21635)
XCTAssertEqual(bNormalized._mantissa.4, 5988)
XCTAssertEqual(bNormalized._mantissa.5, 63852)
XCTAssertEqual(bNormalized._mantissa.6, 1066)
XCTAssertEqual(bNormalized._mantissa.7, 1628)
let result = a + b
XCTAssertEqual(result, Decimal(string: "507.2043458682105010658918477651091")!)
} |
@swift-ci create |
1 similar comment
@swift-ci create |
Fixed in Backport to 5.3 is #2829 $ cat sr-13015.swift
import Foundation
let a = Decimal(string: "119.993")!
let b = Decimal(string: "4.1565")!
let c = Decimal(string: "18.209")!
let d = Decimal(string: "258.469")!
let result = (a * b) + (c * (a / d))
print(result)
print(result == Decimal(string: "507.2043458682105010658918477651091")!)
$~/swift-build/swift-DEVELOPMENT-SNAPSHOT-2020-06-22-a-ubuntu18.04/usr/bin/swift sr-13015.swift
507.2043458682105010658918477651091
true |
Environment
macOS (Catalina 10.15.5):
Linux (Debian stretch, using Ubuntu 16.04 binaries):
en_GB locale
Additional Detail from JIRA
md5: 5c0852cd4db2eecc9ea3b60ca7a075dc
Issue Description:
I've noticed through a rather strange turn of events that there appears to be a problem in Foundation's
Decimal
class on Linux.Take for example this code:
On macOS you'll get the following result:
On Linux you'll get the following result:
My calculator agrees with macOS.
If I change pretty much any of the values for
a
,b
,c
andd
then the result will end up being the same on both platforms. For example, if you changea
to119.99
- so simply removing the final3
- then the result is exactly the same on both platforms.Apologies for the convoluted calculation - I've been trying to get a minimal test case, but whenever I change anything the problem goes away.
The text was updated successfully, but these errors were encountered: