Start here

Home
About Klocwork
What's new
Fixed issues
Release notes
Installation

Reference

C/C++ checkers
Java checkers
C# checkers
MISRA C 2004 checkers
MISRA C++ 2008 checkers
MISRA C 2012 checkers
MISRA C 2012 checkers with Amendment 1
Commands
Metrics
Troubleshooting
Reference

Product components

C/C++ Integration build analysis
Java Integration build analysis
Desktop analysis
Refactoring
Klocwork Static Code Analysis
Klocwork Code Review
Structure101
Tuning
Custom checkers

Coding environments

Visual Studio
Eclipse for C/C++
Eclipse for Java
IntelliJ IDEA
Other

Administration

Project configuration
Build configuration
Administration
Analysis performance
Server performance
Security/permissions
Licensing
Klocwork Static Code Analysis Web API
Klocwork Code Review Web API

Community

View help online
Visit RogueWave.com
Klocwork Support
Rogue Wave Videos

Legal

Legal information

MISRA.FOR.COUNTER.FLT

For loop counter has a floating point type.

MISRA C 2012 Rule 14.1: A loop counter shall not have essentially floating type

Category: Required

Analysis: Undecidable, System

Applies to: C90, C99

Rationale

When using a floating-point loop counter, accumulation of rounding errors may result in a mismatch between the expected and actual number of iterations. This can happen when a loop step that is not a power of the floating-point radix is rounded to a value that can be represented.

Even if a loop with a floating-point loop counter appears to behave correctly on one implementation, it may give a different number of iterations on another implementation.

Example

In the following non-compliant example, the value of counter is unlikely to be 1 000 at the end of the loop.

uint32_t counter = 0u;

for ( float32_t f = 0.0f; f < 1.0f; f += 0.001f )
{
  ++counter; 
}

The following compliant example uses an integer loop counter to guarantee 1 000 iterations and uses it to generate f for use within the loop.

float32_t f;

for ( uint32_t counter = 0u; counter < 1000u; ++counter ) 
{
  f = ( float32_t ) counter * 0.001f; 
}

The following while loop is non-compliant because f is being used as a loop counter.

float32_t f = 0.0f;

while ( f < 1.0f ) 
{
  f += 0.001f; 
}

The following while loop is compliant because f is not being used as a loop counter.

float32_t f; 
uint32_t u32a;

f = read_float32 ( );

do 
{
  u32a = read_u32 ( );
  /* f does not change in the loop so cannot be a loop counter */ 
} while ( ( ( float32_t ) u32a - f ) > 10.0f );

See also

Rule 14.2

MISRA-C++ 2008 Rule 6-5-1 (required): A for loop shall contain a single loop-counter which shall not have floating type.

For loop counter is missing, has floating point type, or there is more than one loop counter.

This rule is also covered by MISRA.FOR.COUNTER.MANY.

Rationale

A for loop without exactly one loop-counter is simply a while loop. If this is the desired behaviour, then a while loop is more appropriate.

Example

y = 0;
for ( x = 0; x < y; x = y++ ) // Non-compliant