kotlin-multiplatform-bignum: Incorrect results of calculations
Describe the bug
Hello. I’m migrating to Kotlin/Native and use your BigNum library as actual
declaration for BigDecimal. on JVM I have Java’s BigDecimal. Many of my tests are failing because the result of calculation on Native with your lib is different.
I provide some examples. Since the migration to native is a long process I can’t know all possible errors with the calculations because I comment failing tests one-by-one. The more I comment, the more errors I see. If I may I’ll add more errors later.
To Reproduce All this values are not equal to what your lib show. So you can just compare both libs and find a cause of different result.
println(BigDecimal("1").divide(BigDecimal("110"), 15,RoundingMode.CEILING ).toPlainString())
println(BigDecimal("6075.6").divide(BigDecimal("0.5"), 0,RoundingMode.CEILING ).toPlainString())
println(BigDecimal("6075.7").divide(BigDecimal("0.5"), 0,RoundingMode.CEILING ).toPlainString())
println(BigDecimal("6075.8").divide(BigDecimal("0.5"), 0,RoundingMode.CEILING ).toPlainString())
println(BigDecimal("6075.9").divide(BigDecimal("0.5"), 0,RoundingMode.CEILING ).toPlainString())
println(BigDecimal("0.001294914096").divide(BigDecimal("0.000001"), 0,RoundingMode.CEILING ).toPlainString())
println(BigDecimal("93545.3695").divide(BigDecimal("1"), 0,RoundingMode.FLOOR ).toPlainString())
Another problem I met is that I can’t leave decimalPrecision
like default value 0 when have a scale
specified. Which is strange because default value 0 should be “unlimited precision” as per the docs. I always have to provide it. I think it’s not right. I only interested in scale and don’t need to limit precision. Right?
Expected behavior I think the results of the lib should be the same as in Java’s BigDecimal
Also there is a difference of how BigNum shows output of toExpandedString in comparison to Java’s solution. It always strips zeroes from the end of a string. Is it expected? If so, why? In some places it’s useful to have a text like “0.00” which gives a hint on scale of the number shown in UI. Maybe it would be a good idea to show zeroes at the end and make another function like toExpandedWithoutZeroes() or even better to change toPlainString() to show zeroes? That for would be great. Both functions will be useful then. What to you think?
Platform Linuxx64, JVM
Additional context Add any other context about the problem here.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 15 (8 by maintainers)
Commits related to this issue
- Rework rounding a bit, fix #168, add two more rounding modes — committed to ionspin/kotlin-multiplatform-bignum by ionspin 3 years ago
- Fix regression whem remainder for rounding is zero, fixes #168 again :) — committed to ionspin/kotlin-multiplatform-bignum by ionspin 3 years ago
- Merge pull request #171 from ionspin/168-additional-fic Fix regression whem remainder for rounding is zero, fixes #168 again :) — committed to ionspin/kotlin-multiplatform-bignum by ionspin 3 years ago
Here you can find Modern Computer Arithmetic downloads https://maths-people.anu.edu.au/~brent/pub/pub226.html
My pleasure!
As far as learning about building the arithmetic library I would say it’s hard to find one place, I listed some of the sources I used at the end of the README, like:
But also by reading from the libraries like GMP, Java BigInteger etc.
Art of Computer programming I mostly just used to get the division algorithm, and I think that my implementation is suboptimal because it relies on 32 bit arithmetic at one point. A lot of room for improvement there, and in the rest of the library in general.
I think Modern Computer Arithmetic is a great starting point, there you can pick up the notation and general concepts. It all boils down to implementing integers, because they you implement arbitrary decimals on top of that. I really don’t consider myself an expert in the area though 😃