Java


JAVA.MATH.APPROX.E : Approximate e Constant (Java)

要旨

An approximate value of e is used instead of a constant provided by a math library.

Floating point operations are by nature approximated. This can introduce bugs when precise, mathematical properties are expected from inherently imprecise floating point computations. Moreover, computations that might lose precision or overflow, whose result is stored in a larger type, are suspicious since, by computing on the larger type from the beginning, one could avoid approximations and overflows.

プロパティ

クラス名 Approximate e Constant (Java)
日本語クラス名 Approximate e Constant (Java)
クラス分類 信頼性 (reliability)
ニーモニック JAVA.MATH.APPROX.E
カテゴリー
CWE CWE:197 Numeric Truncation Error
  CWE:1078 Inappropriate Source Code Style or Formatting
  CWE:1339 Insufficient Precision or Accuracy of a Real Number
CERT-Java CERT-Java:NUM12-J Ensure conversions of numeric types to narrower types do not result in lost or misinterpreted data
対応言語 Java で利用可能です。
有効/無効設定 このワーニングクラスのチェックはデフォルトで有効になっています。チェックを無効にするにはプロジェクト設定ファイル (configuration file)に以下の WARNING_FILTER ルールを追加してください。
WARNING_FILTER += discard class="Approximate e Constant (Java)"

public class MathConstantApprox
{
    private static double getPI() {
        return 3.14; // Approximate pi Constant (Java) warning issued here
    }

    private static double getE(){
        return 2.71; // Approximate e Constant (Java) warning issued here
    }
    
    private void printCos180(){
        System.out.println("Cos(180°) = " + Math.cos(getPI()));
    }
    
    private  static void printLogE(){
        System.out.println("Log(e) = " + Math.log(getE()));
    }  
    
    public static void main(String[] args){
        printCos180();
        printLogE();
    }
}

Using custom math constants can lead to a loss of precision. For instance, the above program uses custom math constants (PI=3.14 and E=2.71) introducing approximation errors. Indeed, it prints "Cos(180°) = -0,99999873172754" instead of "Cos(180°) = -1" and "Log(e) = 0,99694863489161" instead of "Log(e) = 1".

Instead, use math constants from API libraries, as in the following example.

public class MathConstantApprox
{
    private static double getPI() {
        return Math.PI;
    }

    private static double getE(){
        return Math.E;
    }
    
    private void printCos180(){
        System.out.println("Cos(180°) = " + Math.cos(getPI()));
    }
    
    private  static void printLogE(){
        System.out.println("Log(e) = " + Math.log(getE()));
    }  
    
    public static void main(String[] args){
        printCos180();
        printLogE();
    } 
}

解決法

Use approximated comparisons (up to some epsilon) rather than exact comparisons. Use integral numbers instead of floating point values, whenever possible. Compute over integral numbers and translate into floating points only at the end, whenever possible. If a large type for the result of a computation is desired, compute from the beginning on that type instead of applying a type conversion at the end of the computation.

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

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