Weird behaviour for float value in c++.

In the following code why the if condition is giving false, while both the variables prints the same value.

 #include <iostream>
using namespace std;
int main() {
    float b=30;
	float a=29.9917;
	if(b==a+0.0083)
	cout<<"Yes";
	else
	cout<<"No";
	cout<< a+0.0083<<" "<< b;
	return 0;
}

The output of the above code is :
No30 30.

Why is if return false?

try initialising b=30.0000

It is returning false even with b=30.0000

You have to check if absolute difference is negligible or not while comparing floats. Read more here: http://stackoverflow.com/questions/2100490/floating-point-inaccuracy-examples

For example:
#include
using namespace std;

int main() {
    float b=30;
    float a=29.9917;
    if(b-a-0.083 <= 0.00000001)
    cout<<"Yes";
    else
    cout<<"No";
    cout<< a+0.0083<<" "<< b;
    return 0;
}

When you are checking for a condition in if-else, then the absolute difference and the rounding off of the number also matters greatly.

This is explained in floating point theory.

else if you want to eliminate the errors in the code, you can try this,

#include
using namespace std;

int main()
{
float b=30;
float a=29.9917,c;

c=a+0.0083;

if(b==c)
{
    cout<<"Yes";
}
else
{
  cout<<"No";
}

cout<<"\n"<<a+0.0083<<" "<<b<<"\n\n";

return 0;

}