Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rule 13.4 clarification
#1
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.
<t></t>
#2
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]
Posted by and on behalf of the MISRA C Working Group


Forum Jump:


Users browsing this thread: 2 Guest(s)