vendredi 4 juin 2021

Modern practice to compare double/float for equality in modern C++

if (std::abs(double1 - double2) < std::numeric_limits<double>::epsilon())
  std::cout<<"Equal";
else
  std::cout<<"Not equal";

Is this code with modern C++11/14/17/21 is still the way we should compare float and doubles, or now it's ok just to write

if (double1 == double2)

And compiler will handle the epsilon issue for us?

BTW: is it better to write < or <= when checking against epsilon?

Aucun commentaire:

Enregistrer un commentaire