...
The strictfp modifier can be used with a class, method, or interface:
Usage | Applies to |
|---|---|
Class | All code in the class (instance, variable, static initializers), and code in nested classes |
Method | All code within the method |
Interface | All code in any class that implements the interface |
An expression is FP-strict when any of the containing classes, methods, or interfaces is declared to be strictfp. Constant expressions containing floating-point operations are also evaluated strictly. All compile-time constant expressions are by default FP-strict.
...
| Code Block | ||
|---|---|---|
| ||
strictfp class Example {
double d = 0.0;
public void example() {
float f = Float.MAX_VALUE;
float g = Float.MAX_VALUE;
this.d = f * g;
System.out.println("d (" + this.d + ") might not be equal to " +
(f * g));
}
public static void main(String[] args) {
Example ex = new Example();
ex.example();
}
}
|
Exceptions
NUM06NUM53-J-EX0: This rule applies only to calculations that require consistent floating-point results on all platforms. Applications that lack this requirement need not comply.
...
Failure to use the strictfp modifier can result in nonportable, implementation-defined behavior with respect to the behavior of floating-point operations.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
|---|
NUM53-J | low | unlikely | high | P1 | L3 |
Related Guidelines
FLP00-C. Understand the limitations of floating-point numbers | |
VOID FLP00-CPP. Understand the limitations of floating-point numbers |
Bibliography
Ensuring the Accuracy of Floating-Point Numbers | |
[JLS 2005] | |
[JPL 2006] | 9.1.3, Strict and Non-Strict Floating-Point Arithmetic |
Making Deep Copies of Objects, Using strictfp, and Optimizing String Performance |
...
...