 Why does this code shows output false? | SoloLearn: Learn to code for FREE!

+2

# Why does this code shows output false?

https://code.sololearn.com/ck4RVxVmFTm6/?ref=app

+3

This prints true https://code.sololearn.com/cOsZKIAu5gRr/?ref=app

+2

It is risky to compare floating point numbers with the == operator, because it is possible that the values should be equal but they are not because of precision errors. A better way to compare floating point numbers is to assume that two numbers are equal if the difference between them is less than ε, where ε is a small number. In practice, the numbers can be compared as follows (ε = 10^(−9) ): if (abs(a-b) < 1e-9) { // a and b are equal } Note that while floating point numbers are inaccurate, integers up to a certain limit can still be represented accurately. For example, using double, it is possible to accurately represent all integers whose absolute value is at most 2^53

+2

Thanks Arsenic for the help

+2

Asgar Ali what is meant by 0.1f?

+2

Dipra Irham he is telling the compiler that 0.1 I'd floating point number. By default the compiler consider it to be double so it typecast "f1" to double instead which give rize to precision error(as I have already mentioned in my previous answer)

+2

Bro, you need to understand the concept. The values in f1==0.1 are considered to be double precision floating values unless you specify it with 0.1f so the correct code is:- #include <stdio.h> int main() { float f1 = 0.1; if(f1 == 0.1f) printf("True\n"); else printf("False\n"); return 0; }

+1

+1

// Created by Dipra Irham #include <stdio.h> int main() { float f1 = 0.1; if(f1 == 0.1) { printf("True\n");} else{ printf("False\n");} return 0; } Now its correct my dear friend