我已经查了这是做什么,但有人实际上有一个例子,当你在Java中使用strictfp关键字?有人真的发现它的用处了吗?
把它放在所有浮点运算上会有什么副作用吗?
我已经查了这是做什么,但有人实际上有一个例子,当你在Java中使用strictfp关键字?有人真的发现它的用处了吗?
把它放在所有浮点运算上会有什么副作用吗?
当前回答
'strictfp'关键字用于强制Java中的浮点计算(float或double)的精度显式符合IEEE的754标准。如果不使用strictfp关键字,浮点精度取决于目标平台的硬件。
如果接口或类使用strictfp声明,那么该接口或类中的所有方法和嵌套类型都是隐式strictfp。
参考链接
其他回答
也许下面的例子有助于更清楚地理解这一点: 在java中,当我们使用查找任何操作的精确信息时,例如。 如果我们做double num1 = 10e+102;Double num2 = 8e+10; 结果= num1+ num2;
The output will be so long and not precise, becasue it is precissed by the hardware e.g JVM and JIT has the license
as long as we dont have specify it Strictfp
Marking it Strictfp will make the result Uniform on every hardware and platform, because its precised value will be same
One scenario I can see is in a distributed application (or multiplayer game) where all floating-point calculations need to
be deterministic no matter what the underlying hardware or CPU is.
Java 17更新
strictfp的用例集非常狭窄,从Java 17开始,它的功能已经被删除了。它仍然是一个有效的修饰符,但是现在strictfp什么都不做(JLS来源)。
相反,现在所有浮点操作都是严格的,就像在Java 1.2中引入strictfp之前一样。在现代处理器上,不再有任何额外的性能成本。
原来的答案
以下是一些参考资料:
Using strictfp (JDC Tech Tip) jGuru: What is the strictfp modifier for? When would I consider using it? Basically, what it all boils down to is whether or not you care that the results of floating-point expressions in your code are fast or predictable. For example, if you need the answers that your code comes up with which uses floating-point values to be consistent across multiple platforms then use strictfp. strictfp - Java Glossary Floating point hardware calculates with more precision, and with a greater range of values than the Java specification requires. It would be confusing if some platforms gave more precision than others. When you use the strictfp modifier on a method or class, the compiler generates code that adheres strictly to the Java spec for identical results on all platforms. Without strictfp, is it is slightly laxer, but not so lax as to use the guard bits in the Pentium to give 80 bits of precision. And finally the actual Java Language Specification, §15.4 FP-strict Expressions: Within an FP-strict expression, all intermediate values must be elements of the float value set or the double value set, implying that the results of all FP-strict expressions must be those predicted by IEEE 754 arithmetic on operands represented using single and double formats. Within an expression that is not FP-strict, some leeway is granted for an implementation to use an extended exponent range to represent intermediate results; the net effect, roughly speaking, is that a calculation might produce "the correct answer" in situations where exclusive use of the float value set or double value set might result in overflow or underflow.
不过我个人从来没用过它。
我需要它的一次(也是唯一一次)是与IBM ZSeries进行协调。在会计和大型机之外;不。有一段时间了,但我相当确定大型机没有改变。
'strictfp'关键字用于强制Java中的浮点计算(float或double)的精度显式符合IEEE的754标准。如果不使用strictfp关键字,浮点精度取决于目标平台的硬件。
如果接口或类使用strictfp声明,那么该接口或类中的所有方法和嵌套类型都是隐式strictfp。
参考链接
从Java 17+开始,strictfp修饰符已经过时,没有任何作用。你不应该再使用这个修饰符。