I don't know if precomputation is ever guaranteed for those things, but otherwise it would be neat to be able to input rationals directly into the source.
Edit: So, this is scheme standard discovery week: apparently inputing 1/10 works just fine. I can't believe I missed this.
Please research decimal floating point first..
DecFP is not magic. You still need to know that you're dealing with limited precision numbers under the hood.
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.