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

ties are rounded differently on c and js backends #9125

Closed
skilchen opened this issue Sep 29, 2018 · 2 comments · Fixed by #16582
Closed

ties are rounded differently on c and js backends #9125

skilchen opened this issue Sep 29, 2018 · 2 comments · Fixed by #16582

Comments

@skilchen
Copy link
Contributor

On the c-like backends ties (values exactly between 2 adjacent integers) are rounded "away from zero".
On the js backend ties are rounded toward positive infinity.

import math
echo round(-1.5)

produces -2 on the c backend, but -1 on the js backend.

@skellock
Copy link
Contributor

That's interesting that the JS spec calls that out directly:

If the fractional portion is exactly 0.5, the argument is rounded to the next integer in the direction of +∞. Note that this differs from many languages' round() functions, which often round this case to the next integer away from zero, instead giving a different result in the case of negative numbers with a fractional part of exactly 0.5.

Emphasis is theirs.

@skilchen
Copy link
Contributor Author

skilchen commented Oct 2, 2018

Another rounding differenc:
In the stringification all backends round ties "away from zero", but
on the c backends Nim rounds ties to nearest even
on the js backend Nim rounds ties up or down according to the sign of the float.

import strutils
echo formatFloat(2.5, ffDecimal, 0))

produces 2. on the c backend, but 3 on the js backend.
On the c backends we also have the enforced decimal point, which is missing on the js backend.

The round procedure never rounds ties to nearest even: echo round(2.5) produces 3.0 on all backends.

@ringabout ringabout mentioned this issue Dec 30, 2020
1 task
ringabout added a commit to ringabout/Nim that referenced this issue Jan 4, 2021
@ringabout ringabout mentioned this issue Jan 4, 2021
Araq added a commit that referenced this issue Jan 5, 2021
* fix #9125

* Update tests/stdlib/tmath.nim

Co-authored-by: Timothee Cour <timothee.cour2@gmail.com>

* back

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
Co-authored-by: Timothee Cour <timothee.cour2@gmail.com>
mildred pushed a commit to mildred/Nim that referenced this issue Jan 11, 2021
* fix nim-lang#9125

* Update tests/stdlib/tmath.nim

Co-authored-by: Timothee Cour <timothee.cour2@gmail.com>

* back

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
Co-authored-by: Timothee Cour <timothee.cour2@gmail.com>
ardek66 pushed a commit to ardek66/Nim that referenced this issue Mar 26, 2021
* fix nim-lang#9125

* Update tests/stdlib/tmath.nim

Co-authored-by: Timothee Cour <timothee.cour2@gmail.com>

* back

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
Co-authored-by: Timothee Cour <timothee.cour2@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants