top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

How do you compare floating point numbers?

+9 votes
70 views
How do you compare floating point numbers?
posted Nov 18, 2013 by Anuj Yadav

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

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 by Vikas Upadhyay
–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 by Alok Kumar
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 ??

Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...