main()
{
float a=0.7;
if(a<0.7) cout<<"true" ;else cout<<"false";
}
problem---> it prints true .....
when 0.7 is replaced everywhere with 0.8 it displays false
again with 0.9 , it displays true..
typically only for 0.7 and 0.9 it gives different answer ,then the conventional logical answer..why so??

Recommended Answers

All 3 Replies

change your if condition as below

if(a < 0.7f)

I do not remember the exact reason for the behavior in your code, it has got something to do with how floats are treated by the hardware.
Someone please elaborate.

This thread here talks about float comparisons. There's a good link available in the thread about the same.

Thread1

thank you mrinal..
i think it is because the default datatype is double ..hence to do a proper comparison of float values,we have to specify it .
thanks.

change your if condition as below

if(a < 0.7f)

I do not remember the exact reason for the behavior in your code, it has got something to do with how floats are treated by the hardware.
Someone please elaborate.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.