Why output of this code is ''not equal'' ? Please explain! i don't understand this.! | Sololearn: Learn to code for FREE!

+5

# Why output of this code is ''not equal'' ? Please explain! i don't understand this.!

#include <stdio.h> int main() { float f = 0.1; if (f == 0.1) printf ("equal"); else printf ("not equal"); return 0; } Output : not equal

8/1/2021 10:32:20 AM

#...._._...._<........>

+12

It outputs "not equal" because your variable is a float but 0.1 is not. 0.1 is a double by default. To make it float add 'f' at the end. (e.g. 0.1f) This code will output "equal", as required: #include <stdio.h> int main() { float f = 0.1; if (f == 0.1f) printf ("equal"); else printf ("not equal"); return 0; }

+4

Here you decleared one float variable f and you assigning value 0.1 remember that this is double value not float becz by default float value treat as double type but here f type is float and you assigning double value to float variable f . If you type cast lower data type to higher like Short to int ,int to float , float to double. but in case of double to float it means here u converting Higher data type to lower. Both data types have different different ranges so if u will try to assign value it will loose some precision and your data will be loose . If you avoid this kind of problems in your code in c++ you can use static_cast<data type> same thing happening in your case. If u want to define float variable then you can write like this float f=0.1f ; if didn't get answer then try this one your doubts will be clear 0.1is a double value by default when assigned to float it will loose some precision hence it will not compare equal to 0.1 printf("%.20f\n", a); printf("%.20f\n",0.1);

+4

Ooooooh..😅 Great to know. And because of this reason i failed so many times in C programs.

+3

#include <stdio.h> int main() { float a=9.6; printf("%.20f\n", a); printf("%.20f\n",9.6); return 0; }

+2

Aleksei Radchenkov , 😮 Ohh. But in cpp by default any decimal number is double, right ? So is it also in c ?

+2

I don't use C too much, but I believe it's the same as C++

+2

Okay. Anyways, thanks for explaining. 😊

+2

#...._._...._<........> Yeah it will give same in c ,cpp

+2

A.S. , Yeah this statement is working in C -->float f = 0.1f; Thank you so much for this help. and explaining. 😊

+2

#...._._...._<........> are you sure

+2

#...._._...._<........> Just try this: printf("%0.12f\n%0.12f", (double) 0.1f, 0.1); Edit: It should be obvious that this should be included inside the main function.

+2

Saifullah , Thank you. This code is working. 😊

+2

Calvin Thomas , But still i don't understand this. 😅 And its not working on sololearn's compiler. 🙃

+2

User_dead ; Calvin Thomas Sorry both of you. Your given code is right and working. 🙌 that was my mistake, i wrote my code in a wrong way.😬

+2

Just Practice on it then you will be become a good programmer. I am also beginner bit I trying my best for the best.

+1

#...._._...._<........> Try this statement your doubts will be more clear float a=9.6; printf("%.20f\n", a); printf("%.20f\n",9.6);

+1

Whenever you have to deal with floating points number use double data type becz it give more accurate value than float

+1

A.S. , I use this statement [ printf ("%0.20f \n",9.6); ] but this line give me error.

+1

A.S. , Yeah ! Iam sure. Now i just this. These two line gives error.

+1

#...._._...._<........> 🙃😆 Check the code which i have pasted