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.
If you want to represent currency, for example, you should not use decimal floating point. You should integers, specifically you should use an integer number of tenths of cents, which is pretty widely agreed as the standard unit of currency in a computer (or tenths of yen, for example). You need to be extremely careful about overflow, but you need to be anyway, and should almost certainly just use arbitrary-precision integers.
Additionally please research decimal floating points before disagreeing.
For what it's worth, values on the order of 10^18 are exactly representable in int64; decimal64, however, only has 16 significand digits, so there you are already affected by rounding, and it's not far from where the distance between consecutive numbers is greater than 1.
In general, with floating points, you either waste lots of space for excessive precision around zero, or you quickly lose precision when numbers grow. On top of that, you get all the other quirks of floating point numbers such as cancellation.
Or you do have a gram price and are selling literally thousands of tons to a big business
I'm imagining it, and I don't see why storage or computation time would be major obstacles to a calculation you run once a year. Use whatever big integers you want?
Suppose we dedicated one $100 hard drive (per year!) to storing all the relevant data. I feel like there would be plenty of space left over, and the budget would cover it.
Contrary to the "default" floating point, which are base 2 and do not support precise presentations of certain numbers, you can use a decimal floating point system which uses base 10 and therefore allows exact and precise presentation and solves the problem mentioned on the page.
Also important: since 2008 this is standardized in IEEE 754-2008 which added support for decimals.
Explanation of decimal floating points: https://en.m.wikipedia.org/wiki/Decimal_floating_point
Libraries:
https://software.intel.com/en-us/articles/intel-decimal-floa...
http://www.bytereef.org/mpdecimal/
http://speleotrove.com/decimal/
C++ Standard Proposals:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n340...
http://open-std.org/JTC1/SC22/WG21/docs/papers/2014/n3871.ht...