Hacker News new | ask | show | jobs
by namirez 2515 days ago
Fixed-point vs floating-point arithmetic! In scientific applications, you want to preserve precision regardless of the system of units (i.e. scale). For example, 1.23 meters is 1.23e2 cm or 1.23e-3 km. The side effect of this is the roundoff error of arithmetic operations. Floating point operations are not commutative or associative.

In financial transactions on the other hand, we always need only 2 decimal places. So there is no benefit in using floating point.

1 comments

Also in Fintech. In some cases it is actually necessary to have more than 2 decimal places.

For example, interest accrued daily, but only ‘compounded’ monthly. In those cases, it is necessary to maintain far more than the 2 decimal places for the daily accruals.

The best type to use here would be BigDecimal or equivalent. These ultimately serialise to infinite length strings.

Totally agree on the interest use case, that's exactly where precision beyond 2 decimal places is needed. For payments specifically it makes sense to keep it at 2 because all the payment systems in the US have that precision. When we do build support for ledgers we've considered a few approaches for how that might be serialized. For example we sometimes deal with MasterCard data that tags every amount with an exponent field so something like 1.2345 might be serialized as

  { "amount": 12345, "exponent": 4 }
We could do that or something like:

  { "amount": "1.2345" }