+ 4

Why output is false?

INPUT: void main() { float a=0.7; if(a==0.7) printf("true"); else printf("false"); } OUTPUT : false

16th Jun 2017, 10:53 AM
Tanmoy Sau
Tanmoy Sau - avatar
8 Antworten
+ 6
I think...as he said at if condition should be if(a==0.7f) Because 0.7 is double type in "if" while a is float (0.7f is float)
16th Jun 2017, 11:23 AM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
+ 6
I'm not c++ expert though.... So I can only say that double is 8 bytes (64 bit) float is 4 bytes (32 bit) So yes as @sneeze said They both are different
16th Jun 2017, 12:07 PM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
+ 4
He talk about C So It's C
16th Jun 2017, 11:24 AM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
+ 1
Sneeze, it still show the output is false. I want to know that why the output is false. Generally its output come in "true". But why output is "false".
16th Jun 2017, 11:19 AM
Tanmoy Sau
Tanmoy Sau - avatar
+ 1
Data type is different but those values are same, both (0.7).So why if condition doesn't satisfied.
16th Jun 2017, 11:28 AM
Tanmoy Sau
Tanmoy Sau - avatar
+ 1
Default the compiler sees 0.7 as a double. Unless you tell the compiler it is a float. 0.7 double is not the same as 0.7 float because they are stored differently in memory and a double has more precission then a float.
16th Jun 2017, 11:53 AM
sneeze
sneeze - avatar
0
https://code.sololearn.com/c53Xx6zPfk07 The float a=0.7; need a "f" to tell the compiler that 0.7 is a float. I tested it in C# float a=0.7f; Hopes it works the same in the language you are working in.
16th Jun 2017, 11:10 AM
sneeze
sneeze - avatar
0
in which language are you working
16th Jun 2017, 11:23 AM
sneeze
sneeze - avatar