TICS Coding Standard Viewer 
TIOBE Software Quality Framework
Print-friendly version
©TIOBE Software www.tiobe.com
 
ASML C Coding Standard
Search

Rule:  6.5.8.fChecked automatically with code checker

Synopsis:Do not compare floating point numbers with memcmp.
Language:C
Severity Level:8
Category:Expressions


Description:

Justification

Special floating values (e.g. NaN, -0, +0) are not safe to be compared with memcmp. Their logical equality and bit-wise equality differ.

Example

double a = NAN;
double b = NAN;

if( a == b ) /* returns false, not equal */
{
  /* do something */
}

/* WRONG, returns 0 which means equal */
if( memcmp(&a, &b, sizeof(double)) == 0 )
{
  /* do something */
}

double c = 0;
double d = (double) -2 * 0; /* negative zero, -0 */

if( c == d ) /* returns true, equal */
{
  /* do something */
}

/* WRONG, returns 1 which means not equal */
if( memcmp(&c, &d, sizeof(double)) == 0)
{
  /* do something */
}