[Discuss] Financial database / balance?

markw at mohawksoft.com markw at mohawksoft.com
Sun Jan 15 13:56:44 EST 2012


> On Jan 14, 2012, at 1:23 PM, markw at mohawksoft.com wrote:
>>
>> That's a HORRIBLE idea! 3 decimal places are worse that IEEE. If it just
>> a
>> home accounting package, use doubles. If it is for the banking industry
>> use a financial math library.
>
> IEEE-754 is exactly what you don't want to use.  Rounding errors in floats
> and doubles will introduce errors into your calculations.  Here's an
> example:
>
> // some code to print a floating point number to a lot of decimal places
> int main()
> {
>     double f = .37;
>     printf("%.20f\n", f);
> }
> $ gcc -O -o foo foo.c
> $ ./foo
> 0.36999999999999999556
>
> Code snippet modified from:
> http://www.theregister.co.uk/2006/08/12/floating_point_approximation/
>
> As you can see, the internal representation of the number is not the
> number.  It's close, so very close, but "close" is the same as "wrong" in
> accounting.

You are confusing "display" of a double with "use" of a double.
0.36999999999999999556 is numerically, with finite precision less than 15
decimal places, effectively 0.37.

doubles are just fine to use for calculations where <15 decimal places are
all that matter. You just need to be careful on how you round the numbers
up or down for display. You also need to be sure you never use "equals,"
but use greater than and/or less than.

In a home or small business accounting package, doubles should be fine,
you'll only lose or gain a penny occasionally and no one will notice. :-)
ceil() and floor() are your friends.

Financial math uses a different notion of precision than scientific math.
Scientific math will put a man on the moon. Financial math will never
improperly give or take a penny. That's the job of the brokers and
accountants.






More information about the Discuss mailing list