/*
    Demonstrates issue with double comparison.
    
    1) Try changing 1e-15 to 1e-16. What happens and why?
    
    See Nyhoff P53 for representation of floating point numbers
*/

#include <iostream>
#include <cmath>                  // See table P90.

using namespace std;

int main()
{
   double x=3.0;
   double y=x+1e-15;              // 1e-15 = 10^-15
 
// Exact comparison of binary representations.
   if (x==y) cout << "x == y" << endl;
 
// Better comparison, we define = if difference < 1e-8.
   if (fabs(x-y)<1e-8) cout << "x is nearly y" << endl;

   return 0;
}
