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 Java Language Specification ( JLS), §15.4, "FP-strict Expressions" [JLS 2005]:
Wiki Markup theThe net effect \[of non-fp-strict evaluation\], roughly speaking, is that a calculation might produce "the correct answer" in situations where exclusive use of the float value set or double value set might result in overflow or underflow.
...
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 result 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 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.
...
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 compiler optimizations or by designbehavior. 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.
...
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.
...
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(); } } |
The lost magnitude Magnitude loss would also have been lost occur if the value were stored to memory ; – for example, to a field of type float
.
...
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();
}
}
|
...
NUM06-EX1: The strictfp
modifier may be omitted when competent suitable numerical analysis demonstrates that the computed values meet all accuracy and behavioral requirements that are appropriate to the application.
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="9c9eb5f2a8813f99-7a543b72-44544fa2-8bf78b82-51b7b1887f6915c4bfa74562"><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="df262063edf85669-8ffd377b-4893411d-b6c2b6cf-a96790d55c00a23aac270981"><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="10a01293526df0c2-274fed7f-443a4f74-832e8448-b39fb167dc80fe3e6c2ab5dc"><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="f2c2cfa08dc00baa-34d7d037-4139443f-8d50bb18-c17003a6ad6f11ab0470ace2"><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> |
...