views:

842

answers:

5

I'm looking for a fixed-point standard to use for financial data, do you know any that is worth trying? Do you have any experience on the performance of that hand-made fixed-point classes?

+2  A: 

Dr.Dobb's has an article about a possible implementation of fixed-point arithmetic type in C++. Check this out.

Nicola Bonelli
+2  A: 

IBM's decNumber++

Martin Cote
it appears that decNumber++ is all about floating-point, not fixed-point.
Ben Collins
+2  A: 

Here's a really simple and functional implementation.

Eli Bendersky
+2  A: 

Ouch. Financial systems are tricky, your main problem is not fixed point math, the problem are the rounding errors.

You can have a nice spreadsheet full with maverlous calculations with discounts by client type and VAT included. You make a total, you present it to an accountant and he says the values are all wrong. The reason: The output may be formated with only 2 decimal places but internally the value has all the decimal places of a float or double. and they do add up.

You need to know your financials and decide where the base values will be. Meaning what values are the ones the accountants will check (yes it requires business knowledge, hance the 'tricky' part).

The before you save the value to a persistent form (database, file, memory ...) you truncate the extra decimal places that multiplications and divisions may have added.

Quick and dirty solution for N decimal places: ((double)((int)(Value * N * 10.0)))/10.0

Of course you need to check exactly which kind of rounding do your financials require.

Caerbanog
+4  A: 

I use my fixed point math class. It is designed to be more or less a drop in replacement for floats/doubles. http://www.codef00.com/coding.php

Note, the code on my site is for an unsigned fixed point number, if you want signed, just change the uint[N]_t types to int[N]_t types.

Evan Teran