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

Rounding behaves differently with or without the number of decimals. #1404

Closed
jpkolbush opened this issue Feb 15, 2019 · 4 comments
Closed

Comments

@jpkolbush
Copy link

Hello,

I found that rounding behaves differently depending on if you pass in 0 or not.

If you evaluate mathjs.round(-2.5) it returns -2
However if you evaluate mathjs.round(-2.5, 0) it returns -3

We wish to use the later case (always rounding away from 0) so its not a pressing issue, as we can just pass in the extra parameter. But I figured it should be brought to your attention.

@josdejong
Copy link
Owner

That's odd, I would indeed want consistent behavior there. I would expect mathjs.round(-2.5) to return -3 indeed. Thanks for reporting.

Anyone interested in digging into this issue?

@Yaffle
Copy link
Contributor

Yaffle commented Feb 16, 2019

Math.round(-2.5) === -2 :

If two integer Number values are equally close to x, then the result is the Number value that is closer to +∞

while the mathjs.round(-2.5, 0) uses https://github.com/josdejong/mathjs/blob/develop/src/function/arithmetic/round.js#L53 , probably

@josdejong
Copy link
Owner

Turns out round(x) did simply use the built-in Math.round instead of the method used for round(x, n), which allowed for inconsistent behavior.

See fix: 7d00987

Both numbers and BigNumbers now have the following behavior (similar to Matlab and Python, and unlike Mathematica which rounds -2.5 to 2):

math.round(2.5) // number 3
math.round(-2.5) // number -3
math.round(math.bignumber(2.5)) // BigNumber 3
math.round(math.bignumber(-2.5)) // BigNumber -3

@josdejong
Copy link
Owner

Should be fixed now in v5.5.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants