Showing posts with label floating point. Show all posts
Showing posts with label floating point. Show all posts

Tuesday, March 23, 2010

Do NOT check floats for equality




Don't check floats for equality; check for greater than or less than (EqualityFloatLeft.rule)


Description:
This rule checks whether you check floats for equality instead of checking for greater than or less than.

Benefits:

If you check floats for equality, you make your code more susceptible to rounding errors.

Example:

void func(float a, int b)
{
   if (a==b) { }     // Violation

   while (a!=b) { }  // Violation
}



Repair:

void func(float a, int b)
{
   if (a>=b) { }     // OK

   while (a<=b) { }  // OK
}

Author
ParaSoft


My comment for repairing:

void func(const float a, const int b)
{
    if ( a > b ) { }
    else if ( a < b ) {}
    else {}
    // while (a > b) { };
    // while (a < b) { };
}

Ref: http://www.c-faq.com/fp/fpequal.html
Tags: vav.vn, vav, float, float equality, check float values equality, floating point, absolute, epsilon






Labels