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.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.