samedi 25 avril 2020

c++ if condition check of double values gives unexpected output

#include <iostream>

using namespace std;

int
main ()
{
  double a = 0.01;
  double b = 45.01 - 45;
  double c = b;

  if (a == c)
    cout << "a is 0.01" << '\n';
  else
    cout << "a is not 0.01" << '\n';

  return 0;
}

Running the above code gives the output "a is not 0.01" instead of "a is 0.01". I'm not getting why if-statement is messing up the double values of a and c? Here why and a is not equal to b and c? What should I do to get the desired output of "a is 0.01"?

Aucun commentaire:

Enregistrer un commentaire