C and C++


LANG.ARITH.FPEQUAL : Floating Point Equality

Summary

Two floating point values are checked for equality (==) or inequality (!=).

Properties

Class Name Floating Point Equality
Significance style
Mnemonic LANG.ARITH.FPEQUAL
Categories
MisraC2023 MisraC2023:10.1 Operands shall not be of an inappropriate essential type
Misra2012 Misra2012:10.1 Operands shall not be of an inappropriate essential type
Misra2004 Misra2004:13.3 Floating-point expressions shall not be tested for equality or inequality
AUTOSARC++14 AUTOSARC++14:M6-2-2 Floating-point expressions shall not be directly or indirectly tested for equality or inequality.
MisraC++2008 MisraC++2008:6-2-2 Floating-point expressions shall not be directly or indirectly tested for equality or inequality.
CWE CWE:1077 Floating Point Comparison with Incorrect Operator
CERT-C CERT-C:FLP00-C Understand the limitations of floating-point numbers
  CERT-C:FLP02-C Avoid using floating-point numbers when precise computation is needed
JSF++ JSF++:202 Floating point variables shall not be tested for exact equality or inequality.
Availability Available for C and C++.
Enabling Checks for this warning class are disabled by default, and require the unnormalized C ASTs for the project. To enable them, add the following WARNING_FILTER rule and RETAIN_UNNORMALIZED_C_AST specification to the project configuration file.
RETAIN_UNNORMALIZED_C_AST = Yes
WARNING_FILTER += allow class="Floating Point Equality"
Note that retaining the unnormalized ASTs will increase the disk space used to store the project representation, and may make the analysis take longer.

Example

int lang_arith_fpe(float a, float b,
                   double c, double d,
                   long double e, long double f){
   if (a == b){     /* 'Floating Point Equality' warning issued here */
      return 1;
   }
   if (c != d){     /* 'Floating Point Equality' warning issued here
                     * (issued for inequality checks as well as equality checks) */
      return 2;
   }
   if (e == 0.0L){  /* 'Floating Point Equality' warning issued here
                     * (one operand can be a constant) */
      return 3;
   }
   if (f == 0){     /* 'Floating Point Equality' warning issued here
                     * (coercion is taken into account) */
      return 4;
   }
   if (0.0 == 0.0){ /* 'Floating Point Equality' warning issued here
                     * (both operands can be constants) */
      return 5;
   }
}

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.