+ 4
How should I solve this challenge?
What will be the output of the program? float a = 0.7; if(0.7 > a) printf("Hi"); else printf("Hello"); The answer is "Hi". Some decimal numbers can't be represented with 100% accuracy in binary number system. For example: 0.1 or 0.7 can't be, but 0.5, 0.25 can be. How can I know this under 45 seconds? What would be the approach to solve this challenge? Could you explain this snippet, step by step?
3 Answers
+ 5
the answer is "Hi" because
0.7 is a double constant but variable "a" is float.
so whenever we compare a double with floats it leads to type promotion and hence double is greater than float
that's why 0.7 > a
approach to solve this problem
by default floating values have double precision. but if we want to use a value as float then we need to use the keyword "float"
here 0.7 (only) will be stored in memory with double precision and variable a = 0.7 will be stored as float and floats in double precision will be nearly equal to 0.7
see the code below
check it.....đđ
https://code.sololearn.com/ch98HcDVBQLS/?ref=app