Reputation: 105210
I'm having an issue with BigDecimal
s, the simplified idea is to:
total
total
in 3 parts
defined by weights
, these weights are 3 double values that add up to 100.0
parts
total
, the error should be at most 0.00000001Here's the failing test:
@Test
fun sanityCheckExampleForStackOverflow() {
val whole = BigDecimal.valueOf(2_000_000_000.00)
val weights = listOf("25.453778250984232", "35.38647064849812", "39.15975110051765").map { BigDecimal(it) }
val parts = weights.map { weight ->
// w / 100 * total
weight.divide(BigDecimal(100)).times(whole)
}
val sumOfParts = parts[0] + parts[1] + parts[2]
val difference = sumOfParts - whole
assertTrue(difference <= BigDecimal("0.00000001"))
}
What's missing?
Upvotes: 0
Views: 109
Reputation: 108941
Given your weights sum to 100.000000000000002, the value of sumOfParts
is 2000000000.000000040
, which is 0.00000004
from your original value, which is four times bigger than the desired difference of 0.00000001
.
Upvotes: 2