我们如何在实际程序中使用 strictfp 方法?
How can we use the strictfp method in a real life pogram?
此问题与 this question. I know that the strictfp
keyword in java is used for this 目的不重复。但是我们如何在现实生活中使用它。它有什么优势?
自 Java 17 和 JEP 306 起,您不会使用它:
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.
另见 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.
从 Java 的最新 LTE 版本开始,语义上不再有任何差异,并且对使用 strictfp
修饰符有一个新的编译器警告。
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
strictfp in java 用于在处理浮点数时产生相同的结果,与平台无关。
由于硬件的处理能力,不同平台上的精度可能会有所不同。所以它确保了当代码在不同平台上执行时,输出不应该由于不同的精度而被操纵。
一些优点是,
- 在各种机器上提供准确性
- 结果不偏向于硬件架构
此问题与 this question. I know that the strictfp
keyword in java is used for this 目的不重复。但是我们如何在现实生活中使用它。它有什么优势?
自 Java 17 和 JEP 306 起,您不会使用它:
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.
另见 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.
从 Java 的最新 LTE 版本开始,语义上不再有任何差异,并且对使用 strictfp
修饰符有一个新的编译器警告。
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
strictfp in java 用于在处理浮点数时产生相同的结果,与平台无关。
由于硬件的处理能力,不同平台上的精度可能会有所不同。所以它确保了当代码在不同平台上执行时,输出不应该由于不同的精度而被操纵。
一些优点是,
- 在各种机器上提供准确性
- 结果不偏向于硬件架构