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.E : Approximate e Constant (C#)

Summary

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.

Properties

Class Name Approximate e Constant (C#)
Significance reliability
Mnemonic CSHARP.MATH.APPROX.E
Categories
CWE CWE:197 Numeric Truncation Error
  CWE:1078 Inappropriate Source Code Style or Formatting
  CWE:1339 Insufficient Precision or Accuracy of a Real Number
Availability Available for C# only.
Enabling Checks for this warning class are enabled by default. To disable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += discard class="Approximate e Constant (C#)"

Example

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();
        }   
    }
}

Resolution

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.

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.

 

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