Start here

Home
About Klocwork
What's new
Fixed issues
Release notes
Installation

Reference

C/C++ checkers
Java checkers
C# checkers
MISRA C 2004 checkers
MISRA C++ 2008 checkers
MISRA C 2012 checkers
MISRA C 2012 checkers with Amendment 1
Commands
Metrics
Troubleshooting
Reference

Product components

C/C++ Integration build analysis
Java Integration build analysis
Desktop analysis
Refactoring
Klocwork Static Code Analysis
Klocwork Code Review
Structure101
Tuning
Custom checkers

Coding environments

Visual Studio
Eclipse for C/C++
Eclipse for Java
IntelliJ IDEA
Other

Administration

Project configuration
Build configuration
Administration
Analysis performance
Server performance
Security/permissions
Licensing
Klocwork Static Code Analysis Web API
Klocwork Code Review Web API

Community

View help online
Visit RogueWave.com
Klocwork Support
Rogue Wave Videos

Legal

Legal information

MISRA.FLOAT_EQUAL

Floating point expression is tested for equality.

MISRA-C Rule 13.3 (required): Floating-point expressions shall not be tested for equality or inequality.

The inherent nature of floating-point types is such that comparisons of equality will often not evaluate to true even when they are expected to. In addition, the behaviour of such a comparison cannot be predicted before execution, and may well vary from one implementation to another. For example the result of the test in the following code is unpredictable:

float32_t x, y;
/* some calculations in here */
if ( x == y ) /* not compliant */
{ /* ... */ }
if ( x == 0.0f) /* not compliant */

An indirect test is equally problematic and is also forbidden by this rule, for example:

if ( ( x <= y ) && ( x >= y ) )
{ /* ... */ }

The recommended method for achieving deterministic floating-point comparisons is to write a library that implements the comparison operations. The library should take into account the floating-point granularity (FLT_EPSILON) and the magnitude of the numbers being compared.

See also Rule 13.4 and Rule 20.3.

MISRA-C++ Rule 6-2-2 (required): Floating-point expressions shall not be directly or indirectly tested for equality or inequality.

Rationale

The inherent nature of floating-point types is such that comparisons of equality will often not evaluate to true, even when they are expected to. Also, the behaviour of such a comparison cannot be predicted before execution, and may well vary from one implementation to another.

The recommended method for achieving deterministic floating-point comparisons is to write a library that implements the comparison operations. The library should take into account the floating-point granularity (std::numeric_limits<float>::epsilon()) and the magnitude of the numbers being compared.

Example

The result of the test in the following code is unpredictable:

float32_t x, y;

if ( x == y ) // Non-compliant
if ( x == 0.0f ) // Non-compliant

An indirect test is equally problematic and is also prohibited by this rule:

if ( ( x <= y ) && ( x >= y ) ) // Non-compliant

if ( ( x < y ) || ( x > y ) ) // Non-compliant

The following is better, but only if the magnitudes are appropriate:

if ( fabs ( x — y ) <=
   std::numeric_limits<float>::epsilon( ) ) // Compliant