Skip to main content

IAR Embedded Workbench for RL78 5.20

ATH-cmp-float

In this section:
Synopsis

Floating point comparisons using == or !=

Enabled by default

Yes

Severity/Certainty

Low/High

lowhigh.png
Full description

A comparison for equality with a floating-point type uses the == or != operator. This might have an unexpected result because the value of the float varies with the environment and the operation. The comparison might be evaluated incorrectly, especially if either of the floating-point numbers has been operated on arithmetically. In that case, the application logic will be compromised. This check is identical to MISRAC2004-13.3, MISRAC++2008-6-2-2.

Coding standards
CERT FLP00-C

Understand the limitations of floating point numbers

CERT FLP35-CPP

Take granularity into account when comparing floating point values

MISRA C:2004 13.3

(Required) Floating-point expressions shall not be tested for equality or inequality.

MISRA C++ 2008 6-2-2

(Required) Floating-point expressions shall not be directly or indirectly tested for equality or inequality.

Code examples

The following code example fails the check and will give a warning:

int main(void)
{
  float f = 3.0;
  int i = 3;
  
  if (f == i) //comparison of a float and an int
    ++i;
  
  return 0;
}

The following code example passes the check and will not give a warning about this issue:

int main(void)
{
  int i = 60;
  char c = 60;
  
  if (i == c)
    ++i;
  
  return 0;
}