Please explain this | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
1st May 2021, 5:28 PM
Giriraj Yalpalwar
Giriraj Yalpalwar - avatar
22 Answers
+ 7
0.7 is 7/10 Since 10 is not a power of 2 the internal representation used by most computer systems which is binary floating point cannot represent this number exactly. So what the computer actually stores when you enter float a = 0.7; is: 0.699999988079071044921875 which is just an approximation of 0.7 when comparing that to the double value that is stored for 0.7, which is: 0.6999999999999999555910790149937383830547332763671875 which is just a closer approximation of 0.7 you see that the first one is actually a bit less than the latter and that is why this program outputs Hi. This however does not apply to all numbers. So for example the same code with 0.6: float a = 0.6; if(0.6 > a) { printf("Hi"); } else { printf("Hello"); } will print Hello instead and that is because the closest 32 bit representation of 0.6 just happens to be slightly bigger than 0.6 while the 64 bit representation is actually just slightly smaller than 0.6
3rd May 2021, 8:56 AM
Hape
Hape - avatar
+ 5
Hi everyone , I get answer from who created this question. 0.7 is generally without any declare data type is considered as double precision value And double precision value of 0.7 is greater than float 0.7 that's why the output will come as "Hi".
2nd May 2021, 11:23 AM
Giriraj Yalpalwar
Giriraj Yalpalwar - avatar
+ 5
Thirt13n I agree with your answer. I just wanted to give a more detailed view of why this effect happens so people hopefully understand the general working principle of floating point numbers a bit better.
3rd May 2021, 9:09 AM
Hape
Hape - avatar
+ 3
Thirt13n so here in terms of range the value is taken?
2nd May 2021, 12:07 PM
Atul [Inactive]
+ 3
Hey, it is given that a=0.7 and in the code it written as if a>0.7 else return hi so that's why it is hi
3rd May 2021, 10:28 AM
Gvs Charan Gudaru
Gvs Charan Gudaru - avatar
+ 2
Maher Al Dayekh Why float's value is taken as 0.699?
1st May 2021, 6:14 PM
Atul [Inactive]
+ 2
Atul I honestly have no about this, but I think it's about bits or something like that. Hope someone who knows comes and correct me if I'm mistaken. I tried it in C++ like: float a = 0.7f; double b = 0.7; if(a > b) std::cout << "hello"; else std::cout << "hi"; // output : hi
1st May 2021, 6:29 PM
Maher Al Dayekh
Maher Al Dayekh - avatar
+ 2
I think so you are right in saying because when we reverse it,it shows false
1st May 2021, 6:49 PM
Atul [Inactive]
+ 2
Maher Al Dayekh you're right👌🙂
1st May 2021, 8:17 PM
Hacker-KR4636
Hacker-KR4636 - avatar
+ 2
Dear when same value we compare in < or > it only give false output. That's why the output should "Hello".... ☺
3rd May 2021, 8:15 AM
Massom Raza
Massom Raza - avatar
+ 2
Supper
3rd May 2021, 10:06 AM
B.M. Lavan Y. Somarathna.
B.M. Lavan Y. Somarathna. - avatar
+ 2
Here u r writing that if 0.7 is more than a output will be hi and else it would be hello
3rd May 2021, 10:37 AM
Gvs Charan Gudaru
Gvs Charan Gudaru - avatar
3rd May 2021, 10:39 AM
Giriraj Yalpalwar
Giriraj Yalpalwar - avatar
+ 1
a is float so it's actual value is 0.69999998 (not exactly sure about it) and 0.7 is considered as a double (means it stays 0.7) So it's now like: if(0.7 > 0.699) printf("Hi"); // which is true
1st May 2021, 6:11 PM
Maher Al Dayekh
Maher Al Dayekh - avatar
+ 1
Hey Adish Bahirat Read my comment , you will understand ! We are also confused first time 😀
2nd May 2021, 3:03 PM
Giriraj Yalpalwar
Giriraj Yalpalwar - avatar
+ 1
Hey Hape I'm saying that , 0.7 is by default double number , float a=0.7; if(0.7>a) printf("hi"); else printf("Hello"); For this if condition will be evaluates to true because double is greater than float number !
3rd May 2021, 9:02 AM
Giriraj Yalpalwar
Giriraj Yalpalwar - avatar
+ 1
Gvs Charan Gudaru It is if(0.7>a)
3rd May 2021, 10:34 AM
Giriraj Yalpalwar
Giriraj Yalpalwar - avatar
+ 1
0.7 will be in double precision, so the output is Hi, if you want to get Hello as output, typecast like (float)0.7 and compare it.
5th May 2021, 5:12 AM
Vetrivel Subramani
Vetrivel Subramani - avatar
0
Hey Jan Markus But it prints Hi
1st May 2021, 5:47 PM
Giriraj Yalpalwar
Giriraj Yalpalwar - avatar
0
Hello, there The thing is that in that program "a" is already asinged with value "0.7" so if(a>0.7) condition will return false and program will move on to next which is else and will print "Hello". Hope you understand it 😊
2nd May 2021, 6:00 PM
Kedar
Kedar - avatar