Jump to content

Strictfp

From Alfawiki
Adapted from the English Wikipedia article Strictfp, available under CC BY-SA 4.0Updated 9 August 2025
Java
IndustryProgramming language
Founded1995; 31 years ago (1995)
Headquarters
Mountain View
,
United States
OwnerOracle Corporation
Websitewww.oracle.com/java/

strictfp is a deprecated reserved keyword in the Java language. It formerly served as a modifier that constrained floating-point operations to adhere strictly to IEEE 754 standards, ensuring consistent behavior across different platforms. Introduced with JVM version 1.2, this feature was eliminated in JVM 17. Starting from Java 17, compliance with IEEE 754 is mandatory by default, making the keyword redundant.

Basis

The IEEE specification IEEE 754 outlines the standard for performing and storing floating-point arithmetic in formats such as single precision (32-bit, corresponding to Java's float) and double precision (64-bit, corresponding to Java's double).

Certain hardware architectures provide extended precision formats, which offer greater accuracy or wider exponent ranges. While these can reduce round-off errors, overflows, and underflows, they may lead to inconsistent results between different systems. On x86 processors using the traditional x87 floating-point unit, avoiding extended precision incurred performance penalties due to extra instructions needed to restrict exponent ranges.

Prior to JVM 1.2, floating-point computations were strictly confined—intermediate results always fit within IEEE single or double precision—resulting in costly overflow management on x87-based hardware.

Starting with JVM 1.2, intermediate values were allowed to have exponents beyond the standard range by utilizing an "extended-exponent" set. On platforms like x87, this could suppress expected overflows or underflows, producing results that were less predictable but occasionally more accurate.

Since the x87 floating-point unit is no longer mandatory on x86 CPUs supporting SSE2, Java 17 reinstated strict floating-point semantics, reverting to the behavior seen before JVM 1.2.

How it works

When no overflow or underflow occurs, computations yield identical outcomes regardless of strictfp usage. In scenarios demanding deterministic results across environments, applying the strictfp modifier guarantees that overflow and underflow conditions are handled uniformly by enforcing IEEE single and double precision at all intermediate steps, mirroring earlier JVM behavior. Without strictfp, intermediate calculations may utilize an extended exponent range.

Usage

Developers apply the strictfp keyword to enforce IEEE single and double precision compliance in floating-point calculations, ensuring uniform numerical results on all platforms.

This modifier can be attached to classes, interfaces, and concrete methods. When used at the class or method level, all floating-point operations within abide by strict semantics. Compile-time constant expressions inherently follow strict floating-point rules.

Example

<syntaxhighlight lang="java"> public strictfp class MyFPclass {

   // ... contents of class here ...

} </syntaxhighlight>

References