[previous] MISRA-C:2004  Rule  13.4:  (Required) [next] The controlling expression of a for statement shall not contain any objects of floating type.
The controlling expression may include a loop counter, whose value is tested to determine termination of the loop. Floating-point variables shall not be used for this purpose. Rounding and truncation errors can be propagated through the iterations of the loop, causing significant inaccuracies in the loop variable, and possibly giving unexpected results when the test is performed. For example the number of times the loop is performed may vary from one implementation to another, and may be unpredictable.

Example Code:


#include "misra.h"
#include "m2cmex.h"

extern S16 test_1304( void )
{
   F32 f32x;
   S16 s16x = 0;

   for ( f32x = 0.0F; f32x < f32a; f32x++ )   /* MISRA Violation */
   {
      ++s16x;
   }

   return s16x;
}


QAC messages that encompass this guideline:

3340 Floating point variable used as 'for' loop control variable.
3342 Controlling expression of 'for' loop is a floating point comparison.



(c) The Motor Industry Research Association, 2004
QA C Source Code Analyser 8.1.2
MISRA-C:2004 Compliance Module 3.2
© 2013 Programming Research
www.programmingresearch.com
Personality Groups | Glossary | Message Index | MISRA-C:2004 Rule Index Contents