so I was trying a question from codechef contest and the question was pretty simple. The question isn't necessary for my doubt, actually the doubt is, that when I change the data type of the variable "value" from float to double the answer remains the same but the comparison operators work differently
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin >> t;
while (t--)
{
float k1, k2, k3, v;
float value; // <= This line
cin >> k1 >> k2 >> k3 >> v;
//Task
float final = 100.0 / (k1 * k2 * k3 * v);
//Round off to two decimal places
value = (int)(final * 100 + .5);
value = (float)value / 100;
//Check value
cout << value << endl;
//Further task
if (value == 9.58)
{
cout << "yes" << endl;
}
else
{
cout << "no" << endl;
}
}
return 0;
}
Now the answer for the test case
1
1.0 1.0 1.0 10.44
comes out to be
9.58
no
Now if I change the data type of value from float to double
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin >> t;
while (t--)
{
float k1, k2, k3, v;
double value; // <= This line
cin >> k1 >> k2 >> k3 >> v;
//Task
float final = 100.0 / (k1 * k2 * k3 * v);
//Round off to two decimal places
value = (int)(final * 100 + .5);
value = (float)value / 100;
//Check value
cout << value << endl;
//Further task
if (value == 9.58)
{
cout << "yes" << endl;
}
else
{
cout << "no" << endl;
}
}
return 0;
}
The output is
9.58
yes
for the same test case.
So, why is it so, why does the comparison operator work differently even with same value with float and double. Does it have something to do with the round off to two decimal part?