The Java language allows platforms to use available floating-point hardware that can provide extended floating-point support with exponents that contain more bits than the standard Java primitive type double
(in the absence of the strictfp
modifier). Consequently, these platforms can represent a superset of the values that can be represented by the standard floating-point types. Floating-point computations on such platforms can produce different results than would be obtained if the floating-point computations were restricted to the standard representations of float
and double
. According to the JLS, §15§15.4, "FP-strict Expressions" [JLS 2005]:
...
The JVM may choose to treat this case as FP-strict; if it does so, overflow occurs. Because the expression is not FP-strict, an implementation may use an extended exponent range to represent intermediate results.
Code Block | ||
---|---|---|
| ||
class Example {
public static void main(String[] args) {
double d = Double.MAX_VALUE;
System.out.println("This value \"" + ((d * 1.1) / 1.1) + "\" cannot be represented as double.");
}
}
|
...
For maximum portability, use the strictfp
modifier within an expression (class, method, or interface) to guarantee that intermediate results do not vary because of implementation-defined behavior. The calculation in this compliant solution is guaranteed to produce infinity
because of the intermediate overflow condition, regardless of what floating-point support is provided by the platform.
Code Block | ||
---|---|---|
| ||
strictfp class Example {
public static void main(String[] args) {
double d = Double.MAX_VALUE;
System.out.println("This value \"" + ((d * 1.1) / 1.1) + "\" cannot be represented as double.");
}
}
|
...
Native floating-point hardware provides greater range than double
. On these platforms, the JIT is permitted to use floating-point registers to hold values of type float
or type double
(in the absence of the strictfp
modifier), even though the registers support values with greater exponent range than that of the primitive types. Consequently, conversion from float
to double
can cause an effective loss of magnitude.
Code Block | ||
---|---|---|
| ||
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();
}
}
|
...
This compliant solution uses the strictfp
keyword to require exact conformance with standard Java floating-point. Consequently, the intermediate value of both computations of f * g
is identical to the value stored in this.d
, even on platforms that support extended range exponents.
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();
}
}
|
...
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 |
NUM05-J. Do not use denormalized numbers 03. Numeric Types and Operations (NUM)