Boston Linux & Unix (BLU) Home | Calendar | Mail Lists | List Archives | Desktop SIG | Hardware Hacking SIG
Wiki | Flickr | PicasaWeb | Video | Maps & Directions | Installfests | Keysignings
Linux Cafe | Meeting Notes | Blog | Linux Links | Bling | About BLU

BLU Discuss list archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Discuss] Financial database / balance?



> 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.






BLU is a member of BostonUserGroups
BLU is a member of BostonUserGroups
We also thank MIT for the use of their facilities.

Valid HTML 4.01! Valid CSS!



Boston Linux & Unix / webmaster@blu.org