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

Round half to even on 0.5 should be 0 and not 1 #277

Closed
glureau opened this issue Jan 17, 2024 · 1 comment · Fixed by #278
Closed

Round half to even on 0.5 should be 0 and not 1 #277

glureau opened this issue Jan 17, 2024 · 1 comment · Fixed by #278

Comments

@glureau
Copy link
Contributor

glureau commented Jan 17, 2024

Describe the bug
Source of truth: https://en.wikipedia.org/wiki/Rounding (I'm not expert in rounding so if there's something better, tell me)
In this source I can read that the rounding "half to even" of 0.5 should be 0. That's not the case, in pseudo-code I have:

  • roundHalfToEven(0.5, scale = 0) = 1 // Should be 0
  • roundHalfToEven(0.05, scale = 1) = 0 // OK
  • roundHalfToEven(0.005, scale = 2) = 0 // OK

To Reproduce
This test is failing:

    @Test
    fun `rounding half even`() {
        assertEquals(
            0,
            BigDecimal
                .parseString("0.5")
                .roundToDigitPositionAfterDecimalPoint(0, roundingMode = RoundingMode.ROUND_HALF_TO_EVEN)
                .intValue(),
        )
    }

Expected behavior

roundHalfToEven(0.5, scale = 0) == 0

Platform

  • Tested on JVM
@ionspin
Copy link
Owner

ionspin commented Jan 25, 2024

Thanks for reporting!

ionspin added a commit that referenced this issue Jan 28, 2024
…was provided because adding one signum caused rounding to behave as round up. Fixed by adding 2 * signum when round to even is used. Fixes #277.
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 a pull request may close this issue.

2 participants