**¡Nuevo curso!**¡Todo programador debería aprender

**IA Generativa!**

+ 2

# Please explain this

22 Respuestas

+ 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

+ 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".

+ 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.

+ 3

Thirt13n so here in terms of range the value is taken?

+ 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

+ 2

Maher Al Dayekh
Why float's value is taken as 0.699?

+ 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

+ 2

I think so you are right in saying because when we reverse it,it shows false

+ 2

Maher Al Dayekh you're right👌🙂

+ 2

Dear when same value we compare in < or > it only give false output. That's why the output should "Hello".... ☺

+ 2

Supper

+ 2

Here u r writing that if 0.7 is more than a output will be hi and else it would be hello

+ 2

+ 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

+ 1

Hey Adish Bahirat
Read my comment , you will understand !
We are also confused first time 😀

+ 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 !

+ 1

Gvs Charan Gudaru
It is if(0.7>a)

+ 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.

0

Hey Jan Markus
But it prints Hi

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 😊