Semmle 1.21
Skip to end of metadata
Go to start of metadata

Name: Avoid floats in for loops

Description: Floating point variables should not be used as loop counters. For loops are best suited to simple increments and termination conditions; while loops are preferable for more complex uses.

ID: cpp/loop-variable-float

Kind: problem

Severity: recommendation

Precision: high

Query: AV Rule 197.ql
/**
 * @name Avoid floats in for loops
 * @description Floating point variables should not be used as loop counters. For loops are best suited to simple increments and termination conditions; while loops are preferable for more complex uses.
 * @kind problem
 * @problem.severity recommendation
 * @precision high
 * @id cpp/loop-variable-float
 * @tags correctness
 *       reliability
 *       external/jsf
 */
import cpp

from LoopCounter lc
where lc.getUnderlyingType() instanceof FloatingPointType
select lc, "Floating point variables should not be used as loop counters."

This rule finds float variables being used as loop counter. float values are prone to rounding and truncation. In particular, very large and very small float values are prone to rounding errors and could lead to unexpected loop behavior.

Recommendation

Use an integral variable instead of a float variable for the loop counter.

Example

void f() {
	float i = 0.0f;
	//wrong: float used as loop counter
	for (i = 0; i < 1000000.0f; i++) { //may execute 1000000 +x/-x times
		//...
	}
	for (i = 0; i < 100000000.0f; i++) { //may never terminate, as rounding errors 
	                                     //cancel out the addition of 1.0 once 
	                                     //i becomes large enough
		//...
	}
}

References
  • AV Rule 197, Joint Strike Fighter Air Vehicle C++ Coding Standards. Lockheed Martin Corporation, 2005.
  • MISRA C++ Rule 6-5-1, Guidelines for the use of the C++ language in critical systems. The Motor Industry Software Reliability Associate, 2008.
  • FLP30-C. Do not use floating-point variables as loop counters