Home » C programs » C misc. programs

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.






Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.



Comments and Discussions





© https://www.includehelp.com (2015-2018), Some rights reserved.




close Like other websites, this site uses cookies to deliver relevant ads based on your interest, by using our website, you acknowledge that you have read our privacy policy.