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.4, "FP-strict Expressions" [JLS 2005]:
...
Using the strictfp
modifier leaves execution unchanged on platforms that lack platform-specific, extended floating-point support. It can have substantial impact, however, on both the efficiency and the resulting values of floating-point computations when executing on platforms that provide extended floating-point support. On these platforms, using the strictfp
modifier increases the likelihood that intermediate operations will overflow or underflow because it restricts the range of intermediate values that can be represented and the precision of intermediate values; it can also reduce computational efficiency. These issues are unavoidable when portability is the main concern.
...
Usage | Applies to |
---|---|
Class | All code in the class (instance, variable, static initializers), and code in nested classes |
Method | All code within the method is subject to strictness constraints |
Interface | All code in any class that implements the interface is also strict |
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 strictfp
FP-strict.
Strict behavior cannot be is not inherited by a subclass that extends a strictfp
FP-strict superclass. An overriding method can independently choose to be strictfp
FP-strict when the overridden method is not, or vice versa.
...
This noncompliant code example does not mandate strictfp
FP-strict computation. Double.MAX_VALUE
is multiplied by 1.1 and reduced back by dividing by 1.1, according to the evaluation order. If Double.MAX_VALUE
is the maximum value permissible by the platform, the calculation will yield the result infinity
.
However, if the platform provides extended floating-point support, this program might print a numeric result roughly equivalent to Double.MAX_VALUE
.
The initial multiplication might overflow, but is not required to. The JVM may choose to treat this case as strictfp
FP-strict; if it does so, overflow occurs. The ability to use extended exponent ranges Because the expression is not FP-strict, an implementation may use an extended exponent range to represent intermediate values is implementation-definedresults.
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."); } } |
...
Code Block | ||
---|---|---|
| ||
strictfp class Example { public static void main(String[] args) { double d = Double.MAX_VALUE; System.out.println("This value \"" + ((d * 1.1d1) / 1.1d1) + "\" cannot be represented as double."); } } |
This compliant solution also specifies that the floating-point literals (1.1
) are of type double
to clarify their expected type and to maximize their precision.
Noncompliant Code Example
Some platforms provide extended floating-point support in which their native Native floating-point hardware provides greater precision 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();
}
}
|
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="3ef071c9760167d0-c3af29f9-4e06422d-af209869-17a045ba2985a09fadaa51aa"><ac:plain-text-body><![CDATA[ | [[Darwin 2004 | AA. Bibliography#Darwin 04]] | Ensuring the Accuracy of Floating-Point Numbers | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="a39a6ec2f587c917-efc6f3c2-43734201-a5069d35-a102c864d73fbe613a7f7aed"><ac:plain-text-body><![CDATA[ | [[JLS 2005 | AA. Bibliography#JLS 05]] | [§15.4, FP-strict Expressions | http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.4] | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="0bdf635af72f4096-f4553bdb-4b59479c-b111869a-efcf8fad19e6ab908fc132ec"><ac:plain-text-body><![CDATA[ | [[JPL 2006 | AA. Bibliography#JPL 06]] | 9.1.3, Strict and Non-Strict Floating-Point Arithmetic | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="edb6ad190b7270f4-fa8d7cbe-47be48a0-a2dbb032-26e1e62624d12a38f76def7f"><ac:plain-text-body><![CDATA[ | [[McCluskey 2001 | AA. Bibliography#McCluskey 01]] | Making Deep Copies of Objects, Using strictfp, and Optimizing String Performance | ]]></ac:plain-text-body></ac:structured-macro> |
...