Home > Mobile >  How can we use the strictfp method in a real life pogram?
How can we use the strictfp method in a real life pogram?

Time:10-09

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?

CodePudding user response:

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.

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

CodePudding user response:

strictfp in java is used to produce same result irrespective of platform when you are dealing with floating point number.

Precisions may differ on different platform due to hardware's capability for processing it. So it it insures that when the code is executed on different platform the output should not be manipulated due to different precisions.

Some Advantages are,

  • Provides accuracy on various machine
  • Result unbiased to hardware architecture
  • Related