This is probably an old issue but I couldn't find anything satisfactory. I need to check if the result of a fp operation return zero minus all the weird fp behaviours. I tried this and failed miserably, help!
#include <iostream>
#include <cmath>
#include <limits>
#include <iomanip>
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
    double d1 = 0.2;
    double d2 = 1.0;
    double d3 = 0.3;
    d1 = d1 + 0.1;
    d1 = d1 / d3;
    d1 = d1 - d2;
    // print d1 with max precision
    cout << std::setprecision(std::numeric_limits<double>::max_digits10) << d1 << endl;
    // accurately compare d1 with zero with regard to precision
    if (std::abs(d1) < std::numeric_limits<double>::epsilon())
    {
        cout << "d1 is zero" << endl;
    }
    else
    {
        cout << "d1 is not zero" << endl;
    }
    return 0;
}
 
    