Reputation: 41
int main()
{
float lfResult = 19.893196;
if(lfResult == 19.893196)
printf("Works");
else
printf("does not work");
getch();
return 0;
}
Output: does not work
Why does the if
condition fail?
Upvotes: 4
Views: 138
Reputation: 6718
In if condition, 19.893196 can be taken as double. So the if condition fails.
You should try like following way.
if(lfResult == 19.893196f)
I think it will be helpful to you.
Upvotes: 0
Reputation: 60034
your literal is a double, casted to float in assignement.
try:
if(lfResult == 19.893196F)
...
Upvotes: 1
Reputation: 182794
In C floating constants have the type double
. Try:
float lfResult = 19.893196f;
if(lfResult == 19.893196f)
^
Thus the constant 19.893196
has more precision than lfResult
.
6.4.4.2 - 4
An unsuffixed floating constant has type double. If suffixed by the letter f or F, it has type float. If suffixed by the letter l or L, it has type long double.
Upvotes: 8