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

[SR-6885] Double introduces the floating point precision issues #49434

Closed
swift-ci opened this issue Feb 1, 2018 · 1 comment
Closed

[SR-6885] Double introduces the floating point precision issues #49434

swift-ci opened this issue Feb 1, 2018 · 1 comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Feb 1, 2018

Previous ID SR-6885
Radar None
Original Reporter jayant (JIRA User)
Type Bug
Status Resolved
Resolution Invalid

Attachment: Download

Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Bug
Assignee None
Priority Medium

md5: 3d154c69ced19058be55a75c720e18c3

Issue Description:

Not sure if this is a swift bug or a floating point problem and an artifact of using floating point numbers.

I have a Double for total, when I add the amounts to total, the totals are incorrect because the floating point precision changes 135.15 to 135.15000000000009 or 135.149999999999999 or 12.12 becomes 12.119999999999999 which is incorrect

Most of the functions work with Int or Double, so when a decimal number is required, Double it is. However with this little error, the totals and display get muddled up and is a mess.

Sometimes zero is displayed as an exponential number.

These print fine when used with the print statement. However in math operations these cause an issue.

@belkadan
Copy link
Contributor

belkadan commented Feb 1, 2018

That's just how binary floating-point math works; you'll see the same behavior in C, Python, or JavaScript. If you want accuracy with reasonably-precise decimal values, you can look at Foundation.Decimal; otherwise you'll probably have to use a full-on Rational representation. (I believe there are some open-source packages for this.)

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

2 participants