HOME

TheInfoList



OR:

strictfp is an obsolete and redundant reserved word in the
Java programming language Java is a high-level, general-purpose, memory-safe, object-oriented programming language. It is intended to let programmers ''write once, run anywhere'' ( WORA), meaning that compiled Java code can run on all platforms that support Jav ...
. Previously, this keyword was used as a modifier that restricted
floating-point In computing, floating-point arithmetic (FP) is arithmetic on subsets of real numbers formed by a ''significand'' (a Sign (mathematics), signed sequence of a fixed number of digits in some Radix, base) multiplied by an integer power of that ba ...
calculations to
IEEE 754 The IEEE Standard for Floating-Point Arithmetic (IEEE 754) is a technical standard for floating-point arithmetic originally established in 1985 by the Institute of Electrical and Electronics Engineers (IEEE). The standard #Design rationale, add ...
semantics to ensure portability. The strictfp keyword was introduced into Java with the
Java virtual machine A Java virtual machine (JVM) is a virtual machine that enables a computer to run Java programs as well as programs written in other languages that are also compiled to Java bytecode. The JVM is detailed by a specification that formally descr ...
(JVM) version 1.2 and its functionality was removed in JVM version 17. As of Java 17, IEEE 754 semantics is required, thus using this keyword has no effect.


Basis

The
IEEE The Institute of Electrical and Electronics Engineers (IEEE) is an American 501(c)(3) organization, 501(c)(3) public charity professional organization for electrical engineering, electronics engineering, and other related disciplines. The IEEE ...
standard
IEEE 754 The IEEE Standard for Floating-Point Arithmetic (IEEE 754) is a technical standard for floating-point arithmetic originally established in 1985 by the Institute of Electrical and Electronics Engineers (IEEE). The standard #Design rationale, add ...
specifies a standard method for both floating-point calculations and storage of floating-point values in various formats, including single (32-bit, used in Java's float) or double (64-bit, used in Java's double) precision. Some hardware also provides extended precision formats that provide higher precision and/or a larger exponent range. On such architectures, it may be more efficient to compute intermediate results using such extended formats. This may avoid round-off errors, overflows and underflows that would otherwise occur, but can cause programs to produce different output on such architectures. It was particularly expensive to avoid the use of extended precision on x86 machines with the traditional x87 floating-point architecture. Although it was easy to control calculation precision, limiting the exponent range for intermediate results required additional costly instructions. 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 SSE2 (Streaming SIMD Extensions 2) is one of the Intel SIMD (Single Instruction, Multiple Data) processor supplementary instruction sets introduced by Intel with the initial version of the Pentium 4 in 2000. SSE2 instructions allow the use of ...
, Java 17 again made all floating-point operations strict, effectively restoring the pre-1.2 semantics.


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. 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.


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 classes, interfaces, and non-abstract methods. When applied to a method, it causes all calculations inside the method to use strict floating-point math. When applied to a class, all calculations inside the class use strict floating-point math. Compile-time constant expressions must always use strict floating-point behavior. Examples public strictfp class MyFPclass


References

{{Reflist Java (programming language)