ionspin / kotlin-multiplatform-bignum

A Kotlin multiplatform library for arbitrary precision arithmetics
Apache License 2.0
339 stars 40 forks source link

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

Closed glureau closed 5 months ago

glureau commented 5 months ago

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:

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

ionspin commented 5 months ago

Thanks for reporting!