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
Java


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

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 (Java)
Significance reliability
Mnemonic JAVA.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
CERT-Java CERT-Java:NUM12-J Ensure conversions of numeric types to narrower types do not result in lost or misinterpreted data
Availability Available for Java and Kotlin.
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 (Java)"

Example

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

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/.