strictfp
strictfp is an obsolete and redundant reserved word in the
Basis
The
float
) or double (64-bit, used in Java's double
) precision.
Some hardware also provides
Before JVM 1.2, floating-point calculations were required to be strict; that is, all intermediate floating-point results were required to behave as if represented using IEEE single or double precisions. This made it expensive on common x87-based hardware to ensure that overflows would occur where required.
Starting with JVM 1.2, intermediate computations were, by default, allowed to exceed the standard exponent ranges associated with IEEE 32-bit and 64 bit formats. They were permitted to instead be represented as a member of the "extended-exponent" value set. On platforms like x87, overflows and underflows might not occur where expected, producing possibly more meaningful, but less repeatable, results instead.
Since x87 floating point is no longer necessary on x86 processors supporting SSE2, Java 17 again made all floating-point operations strict, effectively restoring the pre-1.2 semantics.[2]
How it works
In the absence of overflow or underflow, there is no difference in results with or without strictfp. If repeatability is essential, the strictfp modifier can be used to ensure that overflow and underflow occur in the same places on all platforms. Without the strictfp modifier, intermediate results may use a larger exponent range.[3]
The strictfp
modifier accomplishes this by representing all intermediate values as IEEE single precision and double precision values, as occurred in earlier versions of the JVM.[4]
Usage
Programmers can use the modifier strictfp
to ensure that calculations are performed as in the earlier versions; that is, only with IEEE single and double precision types used. Using strictfp guarantees that the results of floating-point calculations are identical on all platforms.
It can be used on
Examples
public strictfp class MyFPclass {
// ... contents of class here ...
}
References
- ^ "Java Language Specification - Chapter 3. Lexical Structure".
- ^ a b c "JEP 306: Restore Always-Strict Floating-Point Semantics".
- ^ Gosling, James; Joy, Bill; Steele, Guy L. Jr.; Bracha, Gilad; Buckley, Alex; Smith, Daniel (2017). "4.2.3 Floating-Point Types, Formats, and Values". The Java Language Specification, Java SE 9 Edition. Addison-Wesley Professional. Retrieved 2017-10-06.
- ISBN 978-0-596-00773-7. Retrieved 2010-03-03.
- ISBN 978-0-07-226384-8.
- ISBN 0-321-24678-0. Retrieved 2016-03-22.