I'm doing some Forex work, and considering using this, but don't see any real activity since it was released in 2011. Perhaps I'm looking in the wrong places?
The link you posted said it was specifically aimed at financial applications, so it would probably be fine - seen as comes from Intel.
Decimal floating point (exact decimal, not the traditional binary FP) hasn't quite made it to standard C++ just yet. However the gcc compiler already has it, on my Linux system, the header file is under /usr/include/c++/6.3.1/decimal . It's a standard C++ header so you should be able to #include <decimal/decimal>
I guess this is another option, there may be others it's up to you to decide which you would like to use :+)
are you working with fractions of cents? If not, working with large integer classes (there are tons out there, thanks to encryption stuff) in cents is a great way to deal with money. 128 bit integers fit in a register on modern cpus, and that is a pretty good starting place for most real applications; you can represent over 10^30 dollars as cents with it. Granted, simple solutions don't always fit... if dealing with multiple currencies you generally end up in decimals at some point.
I'm dealing with Forex prices, and so would really like to have _no_ rounding error. I'm now working with a long double, but ending up with prices like:
$124.930000000000006821
Ugh.
A little bird told me that some institutions use two integers to represent prices. They hold the whole number part (USD "dollars") separately from the fractional part (USD "cents") as two ints of some bit length; my guess is 64.
I'm thinking of writing up a class to handle numbers like this, but this is quite an endeavor. I'm hoping that something already exists.
Yeah, don't use floats for money.
At work I was once involved in a project that will remain unnamed that handles Bitcoin transactions, which if you don't know has an indivisible unit (satoshi) of 1E-8 BTC. The code on the side of our client used doubles to track amounts, and they were constantly getting errors of +/- 1 satoshi in transactions. As far as I know, that code is still up, presumably racking up error.
Another easy alternative, as long as you only need elementary operations, is to use two bignums to represent a rational.
right. but 64 bits worth of pennys is a LOT of money.
It is able to represent up to
184467440737095516 dollars as pennies.
I cant see a reason to keep pennies and dollars distinct in today's world, but that was useful when 32 bits was the standard.
For the life of me, unless converting currency (as I said above) I think a simple integer is all you need for most tasks. And there are 128, 256 and larger integers out there if you need them.
2011 is a bit old for paid-for tools that may or may not be supported and may or may not be up to date. That said we used the intel math kernel lib for linear algebra and it was a solid product, didn't need any support and it didn't cost all that much. Only issue was the one we had was 32 bit, and that aged it off, dunno if they replaced it.