Movatterモバイル変換


[0]ホーム

URL:


CodeQL documentation
CodeQL resources

Avoid floats in for loops

ID: cpp/loop-variable-floatKind: problemSecurity severity: Severity: recommendationPrecision: highTags:   - correctness   - reliability   - external/jsfQuery suites:   - cpp-security-and-quality.qls

Click to see the query in the CodeQL repository

This rule findsfloat 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

voidf(){floati=0.0f;//wrong: float used as loop counterfor(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


[8]ページ先頭

©2009-2025 Movatter.jp