我已经查了这是做什么,但有人实际上有一个例子,当你在Java中使用strictfp关键字?有人真的发现它的用处了吗?
把它放在所有浮点运算上会有什么副作用吗?
我已经查了这是做什么,但有人实际上有一个例子,当你在Java中使用strictfp关键字?有人真的发现它的用处了吗?
把它放在所有浮点运算上会有什么副作用吗?
当前回答
Strictfp确保在每个平台上从浮点计算中得到完全相同的结果。如果不使用strictfp, JVM实现可以在可用的地方使用额外的精度。
来自JLS:
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.
换句话说,就是要确保Write-Once-Run-Anywhere实际上意味着write - once - get - equal - wrong - results - everywhere。
使用strictfp,您的结果是可移植的,没有它,它们更可能是准确的。
其他回答
Strictfp确保在每个平台上从浮点计算中得到完全相同的结果。如果不使用strictfp, JVM实现可以在可用的地方使用额外的精度。
来自JLS:
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.
换句话说,就是要确保Write-Once-Run-Anywhere实际上意味着write - once - get - equal - wrong - results - everywhere。
使用strictfp,您的结果是可移植的,没有它,它们更可能是准确的。
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.
不过我个人从来没用过它。
一切都始于一个故事,
When java was being developed by James Gosling, Herbert and rest of his team. They had this crazy thing in mind called platform independency. They wanted to make oak(Java) so much better that it would run exactly same on any machine having different instruction set, even running different operating systems. But, there was a problem with decimal point numbers also known as floating point and double in programming languages. Some machines were built targeting efficiency while rest were targeting accuracy. So, the later(more accurate) machines had size of floating point as 80 bits while the former(more efficient/faster) machines had 64 bit doubles. But, this was against there core idea of building a platform independent language. Also, this might lead to loss of precision/data when a code is built on some machine(having double of 64 bit size) and run on another kind of machine(having double of 80 bit size).
扩大规模是可以容忍的,但缩小规模是不能容忍的。 因此,他们遇到了strictfp的概念,即严格浮点数。如果在类/函数中使用此关键字,则其浮点数和双精度浮点数在任何机器上都具有一致的大小。即分别为32位/64位。
strictfp是一个关键字,可以用作类或方法的非非访问修饰符(但不能用作变量)。将类标记为strictfp意味着类中的任何方法代码都将符合IEEE 754浮点标准规则。
如果没有这个修饰符,方法中使用的浮点可能会以依赖于平台的方式运行。有了它,无论JVM运行在哪个底层平台上,您都可以预测浮点数的行为。缺点是,如果底层平台能够支持更高的精度,那么strictfp方法将无法利用它。
如果您没有将一个类声明为strictfp,那么通过将一个方法声明为strictfp,您仍然可以在逐个方法的基础上获得strictfp行为。
~ SCJP Sun®Java™6认证程序员- Kathy Sierra & Bert Bates ~
strictfp is a modifier which restricts floating point calculations as per IEEE 754. This can be used on whole class like "public strictfp class StrictFpModifierExample{}" or on method "public strictfp void example()".If it is used on class than all methods will follow IEEE 754 and if used on method then particular method will follow IEEE 754. Why it is used??::: As different platforms have different floating point hardware which calculates with more precision and greater range of values than the java specification requires which may produce diffrent output on diffrent plateforms.so it confirms the same output irrespective of diffrent plateforms strictfp also ensures to take advantage of the speed and precision of the extended precision floating-point operations. There is no disadvantage with this keyword we can use when we are doing floating point calculations My last point is --What is IEEE754 in short IEEE 754 defines standard method for both floating point calculations and storage of floating point values in either single (32-bit, used in Java floats) or double (64-bit, used in Java doubles) precision.It also defines norms for intermediate calculations and for extended precision formats.