Click here to Skip to main content
15,902,926 members
Please Sign up or sign in to vote.
1.00/5 (6 votes)
See more:
void main()
{
    float a=0.7;
    if(a<0.7)
    printf("A");
    else
    printf("B");
    getch();
}


If i m putting 0.7 and 0.9 in the question answer is coming "A".else for every option as 0.3,0.5,0.6,0.8 it is coming "B".Why is it so??I know double and float concept.please focus on that,that why it is happening with 0.7 and 0.9 only and not on any other.query is this only,if any one can clear in binary form then it will be most helpful.
Posted
Updated 14-Oct-10 2:29am
v4
Comments
Peter_in_2780 11-Oct-10 22:04pm    
Cross-post! Ask your question ONCE ONLY! Also, it's not a very interesting question.
Sauro Viti 14-Oct-10 8:18am    
Look at the guidelines about posting a question: "3.Keep the subject brief but descriptive."
Your subject doesn't tell us nothinganything about your problem, and also it's not an interesting question.

Decimal round numbers (with finite precision) are not necessarily binary round numbers.

0.7, 0.9 and 0.3 are binary rounded to the precision of their binary representation used by the compiler for double-s.

Now, in your case, the problem is that all numeric literals having a decimal point are treated by the compiler as double. And the precision of double is more than the precision of float.

When you assign a double constant to your a variable (of type float), you loose some of the precision.
Then you convert a back to double (adding bogus bits to its mantissa) to compare it with another constant.

Try either to declare a as double, or to postfix an "f" after the constants (like 0.3f) to make them "float"-s.
 
Share this answer
 
v3
Comments
Aescleal 14-Oct-10 10:28am    
floats are plenty accurate enough to represent differences in the most significant decimal digit of precision. The difference between 0.7 and 0.7f is miniscule (about 2 parts in a 100 million) so suggesting that (a < 0.8) would ever be false because of truncation is a bit bogus.
Emilio Garavaglia 15-Oct-10 3:20am    
Whatever your opinion is, it is matter of fact that "(double)(float)0.7 < 0.7" is true (but in common human sense should be false).
If it is 2 part per million or two millions is irrelevant here.
(or did I misunderstood your comment?)
Program is running correctly I dont know why u are confused
as u have already set the value of a
 
Share this answer
 
Not on the compilers I've just tried it (gcc 3.3 and VC++ 2008) on it's not. With warning levels set as high as they go it won't compile (you're assigning a double to a float) as C++ so that may give you some sort of hint as to what's happening on your compiler and system.

To the muppet that gave me a "1" for this answer: Have you compiled the original posters code and managed to reproduce his results? No, didn't think so...
 
Share this answer
 
v2
Comments
#realJSOP 14-Oct-10 12:07pm    
There is a univoter on the loose. Happens every once in a while.
Because float types are notoriously inaccurate.
Using double would be better until you started doing math with it, and then, all bets are off.

If you want the best possible accuracy, use a decimal type.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900