MISRA Discussion Forums
Rule 13.4 clarification - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C (https://forum.misra.org.uk/forumdisplay.php?fid=4)
+--- Forum: MISRA-C: 2004 rules (https://forum.misra.org.uk/forumdisplay.php?fid=17)
+---- Forum: 6.13 Control Statement Expressions (https://forum.misra.org.uk/forumdisplay.php?fid=40)
+---- Thread: Rule 13.4 clarification (/showthread.php?tid=576)



Rule 13.4 clarification - Alexandre Langenieux - 01-09-2008

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:
extern float32_t foo(void);

int32_t i;
float32_t f;

for (i=0; i 1.0 && f < 3.0) ; i++){
  ...
  f = foo()
  ...
}

Thanks, Alexandre.


Re: Rule 13.4 clarification - misra-c - 08-09-2008

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:
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 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]