MISRA.FLOAT_EQUALFloating 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.RationaleThe 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. ExampleThe 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 |