Discussion

Ask HN: How to handle financial calculations precisely?

I am talking of a Java world, but this extends to any language I think.

Basically the problem is the following: given a set of (positive) floating point numbers, which sum up to x, how do I rescale them in the range [0, 1] so that this new set, when multiplied with x, gives the original set, up to the second decimal place? The max x I expect to encounter is probably in the order of 10000000000, just to be sure.

I've run a quick property based test, and the results are precise only if I use BigDecimal with a MathContext with precision of 8, rounding half up.

Is there some mathematical proof of how much precision I need for this problem?

Edit: sorry the precision was 14, 8 was for a later test with smaller amounts

18 points fedeb95 3 years ago 17 comments

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton