   # How do you compare floating point numbers?

+9 votes
89 views
How do you compare floating point numbers? posted Nov 18, 2013
Share this question

## 2 Answers

+3 votes
``````int compareFloats(float f1, float f2)
{
char *b1, *b2;
int i;

b1 = (char *)&f1;
b2 = (char *)&f2;

/* Assuming sizeof(float) is 4 bytes) */

for (i = 0; i < 4; i++, b1++, b2++)
{
if (*b1 != *b2)
{
return(NOT_EQUAL); /* You must have defined this before */
}
}

return(EQUAL);
}
`````` answer Nov 18, 2013
–1 vote

You can easily compare floats or any other data types variable by using memcmp() .

``````int float_cmp(float *f1, float *f2)
{
return memcmp((void *)f1, (void *)f2, sizeof(float));
}
`````` answer Dec 5, 2013
Similar Questions
+3 votes

Using fprintf() print a single floating point number right-justified in a field of 20 spaces, no leading zeros, and 4 decimal places. The destination should be stderr and the variable is called num.

+7 votes

Write a routine to draw a circle (x ** 2 + y ** 2 = r ** 2) without making use of any floating point computations at all ??