## What is the essential requirement of Rule 13.3 ?

6.13 Control Statement Expressions

Moderators: misra-c, david ward

Gavin McCall
Posts: 72
Joined: Mon Sep 13, 2004 9:24 am

### What is the essential requirement of Rule 13.3 ?

Question: What is the essential requirement of Rule 13.3 ?

Rule 13.3 only prohibits the use of == and !=.

Does this mean that it is legitimate to use the >= and <= operators?

Answer: MISRA C Steering Team 7/9/2005

Do not test for exact equality or exact inequality between floating point values.
Floating point maths is NOT precise.

if ( speed > 10.0f ) /* compliant */
â€¦
if ( speed <= 10.0f) /* compliant */
â€¦
if (speed == 10.0f) /* not compliant */
â€¦
if (speed != 10.0f) /* not compliant */
â€¦

The following forms are equivalent ways of breaking the intent of this rule, but may be beyond the scope of current analysis.

if ((speed < 10.0f) || (speed == 10.0f)) /* not compliant due to test for equality, use <= or < instead. */

Do not form a test for equality with two tests of the following form.

Manual review of all floating point operations involving relational operators may be required.

if ((speed <= 10.0f) && (speed >= 10.0f)) /* not compliant */

if (speed <= 10.0f)
{
if (speed >= 10.0f) /* not compliant */
â€¦

if ( !((speed < 10.0f) || (speed > 10.0f)) ) /* not compliant */
â€¦

If we are testing for a floating point value, test for a range of values, so test for 10.0 to 10.0 + â€œtoleranceâ€, where tolerance is a â€œsmallâ€ number.

if ( ( speed >= (10.0f â€“ tolerance)) && ( speed <= (10.0f + tolerance)))
â€¦

This creates a test around a band of values around 10.0.

See â€œWhat every Computer Scientist should know about floating-point arithmeticâ€ by David Goldberg, March 1991, in Computing Surveys, ACM.
Gavin McCall
Personal view only.