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

Floating Point Rounding #2

Closed
paride opened this issue Aug 12, 2016 · 6 comments
Closed

Floating Point Rounding #2

paride opened this issue Aug 12, 2016 · 6 comments
Labels

Comments

@paride
Copy link

paride commented Aug 12, 2016

I own a Nexus 6 with stock android, so it's a very standard environment.
I found out that it makes some calculation errors:

1/3 = 0.3333333333333333 (ok)
1 + 1/3 = 1.3333333333333333 (ok)
2 + 1/3 = 2.3333333333333335 (wtf?!)
3 + 1/3 = 3.3333333333333335 (wtf?!)
4 + 1/3 = 4.3333333333333333 (ok again until 7+1/3)
8 + 1/3 = 7.3333333333333334 (?!, goes like this until 15+1/3)
16+1/3 = 7.3333333333333338 (and with this I got bored).

This looks bad.
I did a quick test with the Calculator app from Google and the problem is not present there.

@icasdri
Copy link
Owner

icasdri commented Aug 12, 2016

This is an unfortunate side-effect of the fact that computers operate in binary. Any calculation involving floating point numbers have the floating point rounding problem, as it were. See http://floating-point-gui.de/ for more information if you're interested. Try typing the following expression into Google search: 399999999999999-399999999999998. You'll be surprised at the result.

Indeed the real problem here is not that the calculations are wrong, it's the fact that Math.js shows too many (all the digits it knows of in fact) digits by default. I suspect the official Calculator app does not show this many digits. Thus, a solution would involve rounding off Math.js decimal answers.

@icasdri icasdri changed the title Wrong approximations Floating Point Rounding Aug 12, 2016
@icasdri icasdri added the bug label Aug 13, 2016
@paride
Copy link
Author

paride commented Aug 13, 2016

I see, the stock Calculator app indeed trims the result to show less decimal digits.
I often use this calculator (apcalc package on Debian):

http://www.isthe.com/chongo/tech/comp/calc/

which is arbitrary precision.

@icasdri
Copy link
Owner

icasdri commented Aug 13, 2016

Since Mather relies quite extensively on Math.js for calculations and expression parsing, we'll probably need to stick with floating-point numbers and just displaying them in a nicer fashion. Thanks for the info though.

A quick off-topic question: have you tried unit conversions and such. Just as a quick survey about the importance of #3.

@paride
Copy link
Author

paride commented Aug 13, 2016

It seems that arbitrary precision calculations will be implemented in math.js: josdejong/mathjs#694.

@icasdri
Copy link
Owner

icasdri commented Aug 13, 2016

If that happens, we'll inherit that feature :)

@icasdri
Copy link
Owner

icasdri commented Aug 13, 2016

Arbitrary precision fraction support appears to already be available via a configuration option, see http://mathjs.org/docs/core/configuration.html. We will make this available as an option once Settings (#1) is implemented.

@icasdri icasdri closed this as completed in 9a744fb Jan 5, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants