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 some floating-point roundoff errors #21

Merged
merged 5 commits into from
Oct 20, 2020
Merged

Fix some floating-point roundoff errors #21

merged 5 commits into from
Oct 20, 2020

Conversation

amrali-eg
Copy link
Contributor

@amrali-eg amrali-eg commented Oct 14, 2020

I suggest to use the toPrecision() method to preround the result to 15 significant digits. This will strip the floating-point round-off errors in the intermediate calculations.

Please refer to my answer on stackoverflow https://stackoverflow.com/a/48764436

Fixes #20

@sindresorhus
Copy link
Owner

Can you add some tests that would have failed before these changes?

@amrali-eg
Copy link
Contributor Author

Can you add some tests that would have failed before these changes?

roundTo(0.597/6, 3) === 0.1
// false
roundTo.up((0.1+0.2)*10, 0) === 3
// false
roundTo.down((0.1+0.7)*10, 0) === 8
// false

@sindresorhus sindresorhus changed the title Fix for floating-point roundoff errors Fix floating-point roundoff errors Oct 20, 2020
@sindresorhus sindresorhus changed the title Fix floating-point roundoff errors Fix some floating-point roundoff errors Oct 20, 2020
@sindresorhus sindresorhus merged commit de2966e into sindresorhus:master Oct 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Floating-point roundoff errors
2 participants