Decimal Handling in C++

Discussion in 'Automated Trading' started by Craig66, Aug 19, 2008.

  1. Craig66


    I'm in the process of writing a ATS and I am starting to run across problems concerning rounding errors with standard C++ types (float, double) etc. I was wondering how other people using C++ are handling fixed decimal point representation problems of the kind which occur when handling financial data.
  2. Ah that one!

    You need to store the data in numeric format to say 4 fixed decimal places. For this you either need to buy a library that implements this or simply write your own, i.e. a simple wrapper class that stores the number as a string and performs all the math operations as a double as needed.

    It will never be an exact science but it's close enough.

    Hope it helps.

  3. bidask


    lol this is why you don't use the C++. you must roll up your sleeves and explain to it slowly and cleary that 1.00 + 1.00 = 2.00!
  4. That's not C++'s fault, it's simply floating point number representation. You'll run into this with any language.
  5. Yes and no. In Java BigDecimal is available that will do the job. No doubt other languages also address the issue (even COBOL).
  6. GTS


    Use integers. If you want 4 decimal places then multiple everything by 10^4

    Make sure the data type you use can handle the maximum value you will see with the scaling you've selected.
  7. MGJ


    Hire programmers who have read and understood and, ideally, implemented & tested the IEEE spec for floating point arithmetic. (link). Then tell them to store all values and perform all arithmetic in "double" (64 bit) precision floating point numbers, and to use the rounding modes and the library routines ceil(), floor(), etc. in calculations.

    Doubles have a 53 bit mantissa ("fraction part"), giving a precision of 2^53 = 9.0E15. Which means: you get more than 15 decimal digits of precision. As long as you know how to round, you'll be fine.

    By the way, it is not a new idea to store all values as doubles and do all calculations as doubles. The UNIX gang at Bell Labs did this in the original "C" language and its bible The C Programming Language by Kernighan and Ritchie. My copy of the book is Copyright 1978 and I'm sure there are earlier editions.

    FYI the 2007 Gross Domestic Product of all countries on earth, was 5.43E13 US dollars (link) So a 64-bit "double" can hold the GDP of the entire earth, in pennies, with no loss of precision. I suspect this is adequate for your trading needs. As long as you know how to round, you'll be fine.
  8. bidask


    in C# you just use the decimal variable.
    in visual basic you just use float or double and it'll understand.


  9. mtwokay


  10. Craig66


    Thanks for all the answers, I think writing a wrapper class which uses integers as the internal representation sounds like the best idea.

    To the person who reckons c# doubles and floats just work, they don't, this is a rewrite of a c# project and it had the same problem. That time it was solved with strings, but was looking for a more elegant answer.
    #10     Aug 19, 2008