FloatLoopCounter
C++ 検査 への移動
説明
FloatLoopCounter は、浮動小数点値がループ カウンタとして使用された場合に警告します(CERT: FLP30-C、FLP30-CPP)。
浮動小数点数に関するルールによれば、"FLP30-C:浮動小数点変数をループ カウンタに使用しない" となっています。
実装が異なれば、精度の制限も異なります。コードの移植性を保つには、浮動小数点変数をループ カウンタとして使用しないでください。
次のコード例では、浮動小数点変数をループ カウンタとして使用しています。反復の結果は 9 になります。
void __fastcall TForm1::func(void)
{
int Iterations;
Iterations = 0;
for (float x = 0.1f; x <= 1.0f; x += 0.1f)
{
/* Loop may iterate 9 or 10 times */
Iterations = Iterations + 1;
}
ShowMessage("The number of iterations is" +UnicodeString(Iterations);
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
func();
}
次のコード例では、整数変数をループ カウンタとして使用しています。反復の結果は 10 になります。
void __fastcall TForm1::func(void)
{
int Iterations;
Iterations = 0;
for (int x = 1; x <= 10; x += 1)
{
/* 10 回反復処理される */
Iterations = Iterations + 1;
}
ShowMessage("The number of iterations is" +UnicodeString(Iterations);
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
func();
}