Java Strictfp for Always-Strict Floating-Point Semantics

In Java, floating point representations and computations are platform-dependent. The strictfp modifier ensures that all floating-point operations across different JVMs and platforms will provide consistent and the same results predicted by IEEE 754.

When we use strictfp, JVM performs floating-point computations using values that can be represented by a standard Java float or double, guaranteeing that the result of the computations will match exactly across all JVMs and platforms.

Important:

Since Java 17 (JEP 306 – Restore Always-Strict Floating-Point Semantics), Java provide this functionality out of the box everywhere. So, starting with JDK 17, all floating-point operations are consistently strict.

1. The Problem with Floating-point Calculations

In Java, floating-point calculations are not easy and even simple arithmetical properties don’t apply to such calculations. For instance, floating-point additions or multiplications are not associative. In other words (x + y) + z is not equal to x + (y + z) where x, y, and z are real numbers.

Consider the following Java program that tests the associativity of multiplication. The result is ‘false‘ which means that floating-point arithmetic is a methodical approximation of real arithmetic.

double x = 5.899999;
double y = 13.888345;
double z = 14.463534545;

double m1 = (x * y) * z;   // 1185.1596894396725
double m2 = (x * (y * z)); // 1185.1596894396728

System.out.println(m1 == m2);  //false

The main reason behind this approximation is how floating-point calculations produce different outputs on different platforms. To solve this problem, Java has to adopt a rounding policy (by default it is round to the nearest policy). This policy attempts to round an inexact value to a value that is nearest to the infinitely precise result.

In Java, we can solve this issue via the ‘strictfp‘ modifier that was added in Java 1.2.

The strictfp modifier only affects floating-point operations. Integer operations are not affected by strictfp.

2. Where can we apply the ‘strictfp‘ modifier?

In Java, the strictfp modifier can be used in:

  • Class – All code in this class and its nested classes will use strictfp computations.
  • Interface – All classes that implement this interface will use strictfp computations. We can also use it on default methods.
  • Method – All the code within the method will use strictfp computations.
strictfp class MyStrictClass {
    // class body
}

strictfp interface MyStrictInterface {
    // interface body
}

class MyClass {
    strictfp void myStrictMethod() {
        // method body
    }
}

Also, the strictfp modifier can NOT be used in:

  • The variable declarations in a class
  • The constructor declarations in a class
  • The static or instance initialization blocks in a class
  • The ‘abstract‘ classes and methods
  • The ‘abstract‘ methods declared in the interface
strictfp double myVariable;     // Error: strictfp not allowed here

class MyClass {
    strictfp MyClass() {        // Error: strictfp not allowed here
        // constructor body
    }
}

class MyClass {
    static {
        strictfp {
            // static initialization block body      // Error: strictfp not allowed here
        }
    }
}

It is worth noticing that the child class does not inherit this behavior from a parent class. An overriding method (in child class) can independently choose to be strictfp, while the overridden method (in parent class) is not, or vice versa.

3. Java Strictfp Example

Let’s see an example of strictfp modifier for floating point comparisons which will yield exactly same result in all the platforms and processor architectures.

In given example, we have used Double.MAX_VALUE value which is a very big number and has different representations in each platform. Using strictfp, we can ensure that the given calculation will always result in the same value.

public strictfp class Main
{
	public static void main(String[] args)
	{
		double MAX = Double.MAX_VALUE;

		System.out.println(Double.MAX_VALUE - 1);
	}
}

Program output.

1.7976931348623157E308

4. Performance Impact

Using strictfp may have a slight performance impact because it enforces stricter rules for floating-point operations, potentially leading to more expensive calculations.

Although the impact is not that much impactful.

Drop me your questions related to strictfp keyword in Java.

Happy Learning !!

Comments

Subscribe
Notify of
guest
2 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode