Rule 13.4 clarification

6.13 Control Statement Expressions

Moderators: misra-c, david ward

Locked
Alexandre Langenieux
Posts: 2
Joined: Fri Aug 08, 2008 2:19 pm
Company: The MathWorks

Rule 13.4 clarification

Post by Alexandre Langenieux » Mon Sep 01, 2008 4:38 pm

Dear Misra committee,

The rule 13.4 says:
The controlling expression of a for statement shall not contain any objects of floating type.

Does it apply only to loop counter, as suggest in the text explaining the rule ( ... floating-point variables shall not be used for this purpose [loop counter] ...). or to all operand of a controlling expression ?

I mean, can we consider the following for loop as compliant ?

Code: Select all

extern float32_t foo(void);

int32_t i;
float32_t f;

for (i=0; i<187 && (f > 1.0 && f < 3.0) ; i++){
  ...
  f = foo()
  ...
}
Thanks, Alexandre.

misra-c
Posts: 569
Joined: Thu Jan 05, 2006 1:11 pm

Re: Rule 13.4 clarification

Post by misra-c » Mon Sep 08, 2008 2:52 pm

Rule 13.4 states that no objects of floating point type may appear in the controlling expression (i.e. the 2nd expression) of a for statement.
Therefore, the example given in the question violates rule 13.4. The recommended method for introducing additional conditions into the controlling expression is to use a boolean flag. So, the example given could be re-written as:

Code: Select all

extern float32_t foo(void);

int32_t i;
float32_t f;
bool_t flag;

flag = (f > 1.0F && f < 3.0F);
for (i=0; i<187 && flag ; i++){
  ...
  f = foo()
  ...

  flag = (f > 1.0F && f < 3.0F);
}
Using a floating point object to count loop iterations is not recommended. For some values of the loop limits and increment, the accumulation of rounding errors could result in a number of iterations that differs from the expected number.

However, there are contexts in which use of floating point objects in a controlling expression might not cause any issues. MISRA-C:2004 does not permit any such other uses but MISRA will give due consideration to such contexts in a future version of the MISRA C rules. [ID0000010]
---
Posted by and on behalf of
the MISRA C Working Group

Locked

Return to “6.13 Control Statement Expressions”