问题
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?
回答1:
Dr.Dobb's has an article about a possible implementation of fixed-point arithmetic type in C++. Check this out.
回答2:
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.
回答3:
I use my fixed point math class. It is designed to be more or less a drop in replacement for floats/doubles. http://codef00.com/coding
EDIT: As a side note, I would not personally used a fixed point class for this purpose. I would instead just store the number of cents (or tenths of a cent, or hundredths of a cent as needed). A just do the math directly with that. Then I would scale the value appropriately when displaying to the users.
回答4:
IBM's decNumber++
回答5:
ISO specified a decimal extension to C, TR 24732, and to C++, TR 24733. They are available for money on the ISO website. It's not yet part of any published C++ Standard. GCC provides built-in types and a library implementation of it. Another implementation is available from Intel. The most recent push for having this included in C++ is here.
回答6:
A 64-bit int type should suffice for representing all financial values in cents.
You just need to be careful to round percentages correctly, for some definition of correct.
回答7:
Trying to answer directly
Markus Trenkwalder has one that supports some math functions - http://www.trenki.net/content/view/17/1/:
The library consists of various functions for dealing with fixed point numbers (multiplication, division, inversion, sin, cos, sqrt, rsqrt). It also contains a C++ wrapper class which can be used to simplify working with fixed points numbers greatly. I used this fixed point number class in conjunction with my vector_math library to obtain a fixed point vector math library. Doing so made the 3D computations a lot faster compared to the floating point version.
The author made it a point to say his platform does not support floating point though, that's why he did it. Also, note that it's for 3D rendering, the question was for financial data and we want a good library of math functions....
IEEE 754-2008 Decimal Floating-Point Arithmetic specification, aimed at financial applications
This looks like an established way of handling financial data with good support (from Intel and IEEE) - http://software.intel.com/en-us/articles/intel-decimal-floating-point-math-library
To quote:
IEEE 754-2008 Decimal Floating-Point Arithmetic specification, aimed at financial applications, especially in cases where legal requirements make it necessary to use decimal, and not binary floating-point arithmetic (as computation performed with binary floating-point operations may introduce small, but unacceptable errors).
It is NOT fixed-point though, but I thought it is pretty useful for people seeking an answer to this question.
来源:https://stackoverflow.com/questions/146207/what-do-you-use-for-fixed-point-representation-in-c