我已经查了这是做什么,但有人实际上有一个例子,当你在Java中使用strictfp关键字?有人真的发现它的用处了吗?

把它放在所有浮点运算上会有什么副作用吗?


当前回答

'strictfp'关键字用于强制Java中的浮点计算(float或double)的精度显式符合IEEE的754标准。如果不使用strictfp关键字,浮点精度取决于目标平台的硬件。

如果接口或类使用strictfp声明,那么该接口或类中的所有方法和嵌套类型都是隐式strictfp。

参考链接

其他回答

一切都始于一个故事,

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'关键字用于强制Java中的浮点计算(float或double)的精度显式符合IEEE的754标准。如果不使用strictfp关键字,浮点精度取决于目标平台的硬件。

如果接口或类使用strictfp声明,那么该接口或类中的所有方法和嵌套类型都是隐式strictfp。

参考链接

正如其他答案所提到的,它导致中间浮点结果符合IEEE规范。特别是x86处理器可以存储与IEEE规范不同精度的中间结果。当JIT优化特定的计算时,情况变得更加复杂;指令的顺序可能每次都不同,导致舍入略有不同。

由strictfp引起的开销可能非常依赖于处理器和JIT。 这篇关于SSE2的维基百科文章似乎对这个问题有一些见解。 因此,如果JIT可以生成SSE指令来执行计算,那么strictfp似乎不会有任何开销。

在我当前的项目中,有几个地方我使用了strictfp。有一点,潜在的宇宙射线需要从像素值中去除。如果一些外部研究人员在他们面前有相同的像素值和宇宙射线,他们应该得到与我们的软件相同的结果值。

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.

我需要它的一次(也是唯一一次)是与IBM ZSeries进行协调。在会计和大型机之外;不。有一段时间了,但我相当确定大型机没有改变。