C and C++


LANG.ARITH.FPEQUAL : 浮動小数点を用いた等価比較

要旨

浮動小数点を持つ値同士で、比較 (==) (!=)を行っています。

プロパティ

クラス名 Floating Point Equality
日本語クラス名 浮動小数点を用いた等価比較
クラス分類 スタイル (style)
ニーモニック LANG.ARITH.FPEQUAL
カテゴリー
MisraC2023 MisraC2023:10.1 Operands shall not be of an inappropriate essential type
Misra2012 Misra2012:10.1 Operands shall not be of an inappropriate essential type
Misra2004 Misra2004:13.3 Floating-point expressions shall not be tested for equality or inequality
AUTOSARC++14 AUTOSARC++14:M6-2-2 Floating-point expressions shall not be directly or indirectly tested for equality or inequality.
MisraC++2008 MisraC++2008:6-2-2 Floating-point expressions shall not be directly or indirectly tested for equality or inequality.
CWE CWE:1077 Floating Point Comparison with Incorrect Operator
CERT-C CERT-C:FLP00-C Understand the limitations of floating-point numbers
  CERT-C:FLP02-C Avoid using floating-point numbers when precise computation is needed
JSF++ JSF++:202 Floating point variables shall not be tested for exact equality or inequality.
対応言語 C および C++ で利用可能です。
有効/無効設定 このワーニングクラスのチェックはデフォルトで無効になっており、プロジェクトには非正規の C向けAST が必要になります。有効にするにはプロジェクト設定ファイル (configuration file) に以下の WARNING_FILTER ルールと RETAIN_UNNORMALIZED_C_AST 設定を追加してください。
RETAIN_UNNORMALIZED_C_AST = Yes
WARNING_FILTER += allow class="Floating Point Equality"
注:非正規化された AST を継続して使用した場合、使用ディスク容量が増加し解析時間が長くなる可能性があります。

int lang_arith_fpe(float a, float b,
                   double c, double d,
                   long double e, long double f){
   if (a == b){     /* 'Floating Point Equality' warning issued here */
      return 1;
   }
   if (c != d){     /* 'Floating Point Equality' warning issued here
                     * (issued for inequality checks as well as equality checks) */
      return 2;
   }
   if (e == 0.0L){  /* 'Floating Point Equality' warning issued here
                     * (one operand can be a constant) */
      return 3;
   }
   if (f == 0){     /* 'Floating Point Equality' warning issued here
                     * (coercion is taken into account) */
      return 4;
   }
   if (0.0 == 0.0){ /* 'Floating Point Equality' warning issued here
                     * (both operands can be constants) */
      return 5;
   }
}

関連のある設定ファイルパラメータ

設定ファイルの以下のパラメータがこのワーニングクラスのチェックに影響します。