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 |
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.
| Class Name | Approximate pi Constant (Java) | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Significance | reliability | ||||||||||||
| Mnemonic | JAVA.MATH.APPROX.PI | ||||||||||||
| Categories |
|
||||||||||||
| 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 pi 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.
The following configuration file parameters affect checks for this warning class.
To report problems with this documentation, please visit https://support.codesecure.com/.