fork download
  1. #include <iostream>
  2. #include <cmath>
  3. #include <limits>
  4.  
  5. bool AreDoubleSame(double dFirstVal, double dSecondVal)
  6. {
  7. return std::abs(dFirstVal - dSecondVal) < std::numeric_limits<double>::epsilon();
  8. }
  9.  
  10.  
  11. int main()
  12. {
  13. double dFirstDouble = 0.200001;
  14. double dSecondDouble = 20000.1 / 100000.0;
  15.  
  16. std::cout << "AreDoubleSame: ";
  17. if(AreDoubleSame(dFirstDouble , dSecondDouble) )
  18. {
  19. std::cout << "equal " << std::endl;
  20. } else {
  21. std::cout << "not equal" << std::endl;
  22. }
  23. std::cout << "==: ";
  24. if(dFirstDouble == dSecondDouble)
  25. {
  26. std::cout << "equal " << std::endl;
  27. } else {
  28. std::cout << "not equal" << std::endl;
  29. }
  30. }
Success #stdin #stdout 0.01s 2724KB
stdin
Standard input is empty
stdout
AreDoubleSame: equal 
==: not equal