What is the essential requirement of Rule 13.3 ?

6.13 Control Statement Expressions

Moderators: misra-c, david ward

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

What is the essential requirement of Rule 13.3 ?

Post by Gavin McCall » Fri Sep 09, 2005 2:39 pm

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.

Locked

Return to “6.13 Control Statement Expressions”