C Example for different floating point values prediction

In this example, we are going to learn about float value’s behavior how they assigned and why they behave when comparing in C programs?
Submitted by Shamikh Faraz, on February 25, 2018

In this code, floating values are same, but their data types are different. The compiler checks and declares them ‘not equal’ on the basis of data types.

#include <stdio.h>

int main()
{
    float a=21.2;
    double b=21.2;
	
    if (a==b)
	    printf("Both values are equal");
    else
	    printf("Both values are not equal");

    return 0;	
}

Output

Both values are not equal

"Both values are equal", you are thinking the same. But the output says just opposite. "Both are not equal" , this is because, in floating point number like float, double, long double, the values cannot be predict exactly, these are depending on the number of bytes. So the compiler differences between 21.2 and 21.2.

C Basic Programs »


Related Programs

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.