Add "decimal" support to the standard C++ library
Among the most challenging problems in a financial system is dealing with numeric precision.
Take this example for instance (using the double data type in C++):
We have a number 4.3599999999999994 which we withdraw the same negative number from (4.3599999999999977), we would then end up with the number 1.7763568394002505e-015. Hence, if we were testing if the number added up to 0, we would get "False", or "True" if we were testing if the number was greater than "0".
This could easily get the system into an unwanted "IF" statement,
and tracking down these unwanted "IF" statements can be difficult.
Since C++ is used in many financial systems where accurate numeric precision is required, it seems reasonable to add "decimal" support to the standard C++ library.
Ben Voigt [Visual C++ MVP] commented
Support the similar proposal that's based in the actual Standard, instead. https://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/2433730-add-std-decimal-with-iso-iec-tr-24733-decimal-flo
Andy Bolstridge commented
Agreed, if you want a standard C++ decimal type, write one and submit it to Boost, or appeal to the C++ committee. In the meantime, use a 64-bit int, store pennies (or fractions of) in there.
I think this is a great idea.
Almost all languages support decimal floating point arithmetic.
Java by java.math.BigDecimal, Python by decimal.Decimal, Ruby by BigDecimal and .NET by System.Decimal which is also accessible in C# as decimal.
The document ISO/IEC TR 24733 specifies an extension for the programming language C++ to support decimal floating-point arithmetic.
Unfortunately, the Decimal TR (ISO/IEC TR 24733) was not accepted into C++11. However, there is already a proposal N3407 to add decimal floating point support to the C++ standard. The proposal indicates to accept it for C++17.
Hopefully Decimal TR (ISO/IEC TR 24733) will be provided by the C++ Standard Library shipped with Visual C++ as soon as possible.
The GNU C compiler already supports decimal floating types as defined in the N1312 draft of ISO/IEC WDTR24732.
Lars Wilhelmsen commented
Yeah, I agree - this is definitely a -1.
Brian Webb commented
Google C++ Decimal Struct. There are open source decimals that are structs. They are all integer based, with an N coefficient, which is what you want when you don't want a float.
Or use the .NET one in System.
I'd -1 this if I could, if you want to change the standard C++ libraries give some feedback to the C++ standards comittee.