This question already has an answer here:
- Is floating point math broken? 22 answers
Let's take this code
#include<iostream>
#include<conio.h>
int main()
{
float x = 3.1;
double y = 3.1;
if(x==y)
{
cout<<"Yes";
getch();
}
else
{
cout<<"No";
getch();
}
return 0;
}
So apparently, this code should print "Yes" on the screen but it does not. It just skip the x==y condition even x and y are equal and execute the else statement. Why can't we compare float and double. Do I need to use type casting or something else like that?
Aucun commentaire:
Enregistrer un commentaire