|
|
|
|
|
by kirab
3368 days ago
|
|
You are right, that example doesn't work. I thought the rounding and normalization described in the standard may fix all these cases by itself but there I was wrong. But at least all problems that could happen on a financial application are solved with decimal floating points (where you will only want to use rationals in finite decimal form like 0.01) And even your example can be made working pretty easily: #include <decimal/decimal>
int main(int /*argc*/, char **/*argv*/)
{
using namespace std;
using namespace decimal;
decimal128 d1 = 1;
decimal128 d2 = 2;
decimal128 d7 = 7;
uint64_t conversionFactor = 10000000000000000000ull;
cout << decimal128_to_long_long(d1/d7 * conversionFactor) << endl;
cout << decimal128_to_long_long(d2/d7 * conversionFactor) << endl;
cout << (decimal128_to_long_long((d1/d7+d1/d7) * conversionFactor) ==
decimal128_to_long_long((d2/d7) * conversionFactor) ? "yes" : "no") << endl;
}
1428571428571428571
2857142857142857142
yes
That was done using the gcc included decimal types. And if you look e.g. into the intel dfp library readme, you see lots of functions which will allow you to do the comparison you wanted to do:
https://software.intel.com/sites/default/files/article/14463... |
|
You still get cancellation if the magnitudes differ by large enough an amount. This is a problem inherent to floating point arithmetic, using a decimal format instead of binary does not save you from that.