I have a c++ ledger application in which floating point is used for calculations, now what should I do to convert to fix point arthimatic (considerably upto 4 digits past decimal point) without generating more bugs in the program. Any step wise process which I should adopt or tips to prevent errors? Please suggest some test cases which will be helpful
Asked
Active
Viewed 191 times
0
-
2Creating a suite of unit tests to cover all the current calculations in your application would be a good start. It would be a good to be able to swap between fixed and floating point easily in this scenario, so that you can compare results easily. – Simon Bosley Mar 12 '14 at 10:38
1 Answers
1
- Introduce a type
Currency
used in relevant computations (if not done already) - Make sure all relevant numbers are stored as
Currency
, not asdouble
, orfloat
- Define
Currency
with a fixed-point real type. You can use existing implementations, like CodeF00'snumeric::Fixed
. See also What's the best way to do fixed-point math?
-
Please suggest tests and tips to avoid performance issues as well.:) – user3172833 Mar 13 '14 at 03:22
-
I looked at your reference, sorry to be naive but it says that according to your architecture(bitsize) you can typedef fixed class. For eg typedef fixed<16,16> fixed. Will it affect if I shift my data to different architectures.Any way to keep it uniform. – user3172833 Mar 13 '14 at 03:56
-
Test tips are very application-specific. Cannot say anything non-obvious having no information on your case. Concerning your second comment - don't hesitate to use question marks. – Mikhail Mar 13 '14 at 07:01
-
Well I have used GMP lib for a double entry ledger system. I agree that you will need to know more features of the system to describe the tests. But a test which proves whether error exist due to current floating point arthimatic will be fine. I wanted tips on how is performance affected if i use fixed point arithematic on top of GMP vs MPFR of GMP. – user3172833 Mar 18 '14 at 12:03