Confusing questing
-
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 the float and double concept...dont tell that...but why is difference in answers ..please focus on that.
-
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 the float and double concept...dont tell that...but why is difference in answers ..please focus on that.
mstanwar wrote:
I know the float and double concept
do you really? 0.7 is a real number; it cannot be represented exactly as it is not equal to an integer times some power of 2. therefore, it has an inherent inaccuracy. furthermore, it is a double (real constants are double by default in C-like languages); assigning it to a float enlarges the inaccuracy, as there are fewer mantissa bits in a float than there are in a double.
a<0.7
is an expression that gets evaluated as doubles (see the language specification); so you are basically comparing the float representation and the double representation of the number 0.7, and those are both approximations, and unequal, and either one may be the larger one. If you really knew about floats and doubles, you wouldn't carelessly mix them like that. :)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.