08-09-2008, 01: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:
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]
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