If float and double are not accurate, how do banks perform accurate calculations involving money?

bank, c++, currency

Solution

In many cases, financial calculations are done using fixed-point arithmetic instead of floating point.

For example, the .NET `Decimal` type, or the VB6 `Currency` type. These are basically just integer types, where everyone has agreed that the units are some fraction of a cent, like $.0001.

And yes, some rounding has to occur, but it is done very systematically. Usually the rounding rules are somewhere deep in the fine print of your contract (the interest rate is x%, compounded every T, rounded up to the nearest penny, but not less than $y every statement period).

Problem

Currently learning C++ and this has just occurred to me. I'm just curious about this as I'm about do develop a simple bank program. I'll be using `double` for calculating dollars/interest rate etc., but there are some tiny differences between computer calculations and human calculations. I imagine that those extra .pennies in the real world can make all the difference!

Original source

Related problems