JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.

If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.

If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.

CodeSonar® 9.2p0 CONFIDENTIAL CodeSecure Inc
C#


CSHARP.MATH.APPROX.PI : Approximate pi Constant (C#)

要旨

An approximate value of π 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 pi Constant (C#)
日本語クラス名 Approximate pi Constant (C#)
クラス分類 信頼性 (reliability)
ニーモニック CSHARP.MATH.APPROX.PI
カテゴリー
CWE CWE:197 Numeric Truncation Error
  CWE:1078 Inappropriate Source Code Style or Formatting
  CWE:1339 Insufficient Precision or Accuracy of a Real Number
対応言語 C# で利用可能です。
有効/無効設定 このワーニングクラスのチェックはデフォルトで有効になっています。チェックを無効にするにはプロジェクト設定ファイル (configuration file)に以下の WARNING_FILTER ルールを追加してください。
WARNING_FILTER += discard class="Approximate pi Constant (C#)"

using System;

namespace MathConstantApproxExamples
{
    public class MathConstantApprox
    {
        private double GetPI() {
            return 3.14; // Approximate pi Constant (C#) warning issued here
        }

        private double GetE()
        {
            return 2.71; // Approximate e Constant (C#) warning issued here
        }
        
        private void PrintCos180(){
            Console.WriteLine("Cos(180°) = " + Math.Cos(GetPI()));
        }
        
        private void PrintLogE(){
             Console.WriteLine("Log(e) = " + Math.Log(GetE()));
        }  
        
         static void Main(string[] args)
        {
            PrintCos180();             // prints "Cos(180°) = -0.99999873172754" (should be -1)
            PrintLogE();               // prints "Log(e) = 0.99694863489161" (should be 1)
    
        } 

    }

}

The programmer can address this program by using Math constants instead of hardcoded approximations.

using System;

namespace MathConstantApproxExamples
{
    public class MathConstantApprox
    {
        private double GetPI() {
            return Math.PI;
        }

        private double GetE()
        {
            return Math.E;
        }
        
        private void PrintCos180(){
            Console.WriteLine(Math.Cos(GetPI()));
        }
        
        private void PrintLogE(){
             Console.WriteLine(Math.Log(GetE()));
        } 
        
        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.

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

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

 

To report problems with this documentation, please visit https://support.codesecure.com/.