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

BigDecimal divideAndRemainder wrong results #276

Open
sergeych opened this issue Jan 4, 2024 · 3 comments
Open

BigDecimal divideAndRemainder wrong results #276

sergeych opened this issue Jan 4, 2024 · 3 comments
Assignees

Comments

@sergeych
Copy link

sergeych commented Jan 4, 2024

Describe the bug
The result of BigDecimal.divideAndRemainder are incorrect.:

 @Test
    fun testRemainder() {
        val full = BigDecimal.fromInt(360)
        val x = BigDecimal.fromDouble(15.5) + full*5

        val (q,r) = x.divideAndRemainder(full)
        assertEquals("5", q.toStringExpanded())  // got: 5.043 
        assertEquals("15.5", r.toStringExpanded())
    }
  • quotient must be a whole number first of all. The remainder is invalid too.

Platform

  • [JVM]

Additional context

I've checked it against java math BigDecimal, it works as expected:

    @Test
    fun testRemainder2() {
        val full = java.math.BigDecimal(360)
        val x = java.math.BigDecimal(15.5) + full*java.math.BigDecimal(5)

        val divrem = x.divideAndRemainder(full)
        assertEquals("5.0", divrem[0].toPlainString())
        assertEquals("15.5", divrem[1].toPlainString())
        // and the shortcut:
        assertEquals("15.5", (x % full).toPlainString())
    }

And thanks for this library and Happy New Year!

@ionspin ionspin self-assigned this Jan 5, 2024
@ionspin ionspin closed this as completed in 565ad3d Jan 5, 2024
@ionspin
Copy link
Owner

ionspin commented Jan 5, 2024

The precision needed for rounding was not calculated properly. The fix should be available in snapshot soon. Thanks for reporting and happy new year as well!

@makaronis
Copy link

makaronis commented May 7, 2024

It's still counting wrong, if number is less then 1

`
@test
fun roundNewText() {
val one = "0.59".toBigDecimal()
val two = "0.1".toBigDecimal()

    val result = one.divideAndRemainder(two)

    println("result=${result.second.toPlainString()}")
    // Result is "0.59"
}

@Test
fun roundNewText() {
    val one = "1.59".toBigDecimal()
    val two = "0.1".toBigDecimal()

    val result = one.divideAndRemainder(two)

    println("result=${result.second.toPlainString()}")
    // Here result is 0.09
}

`

@ionspin
Copy link
Owner

ionspin commented May 9, 2024

Thanks for reporting, I'll check it out when I have some free time.

@ionspin ionspin reopened this May 9, 2024
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

No branches or pull requests

3 participants