This question is not a duplicate of this question. I know that the strictfp
keyword in java is used for this purpose. But How can we use this in a real life application. What are its advantages?
As of Java 17 and JEP 306, you wouldn't use it:
Make floating-point operations consistently strict, rather than have both strict floating-point semantics (
strictfp
) and subtly different default floating-point semantics.[T]he SSE2 (Streaming SIMD Extensions 2) extensions, shipped in Pentium 4 and later processors starting circa 2001, could support strict JVM floating-point operations in a straightforward manner without undue overhead.
Since Intel and AMD have both long supported SSE2 and later extensions which allow natural support for strict floating-point semantics, the technical motivation for having a default floating-point semantics different than strict is no longer present.
See also JLS §8.4.3.5:
The
strictfp
modifier on a method declaration is obsolete and should not be used in new code. Its presence or absence has has no effect at run time.
As of the latest LTE release of Java, there is no longer any difference in semantics and there is a new compiler warning for the use of the strictfp
modifier.
public class Strictly {
public strictfp double calc(double d1, double d2) {
return d1 * d2;
}
}
$ javac Strictly.java Strictly.java:2: warning: [strictfp] as of release 17, all floating-point expressions are evaluated strictly and 'strictfp' is not required public strictfp double calc(double d1, double d2) { ^ 1 warning