我已经查了这是做什么,但有人实际上有一个例子,当你在Java中使用strictfp关键字?有人真的发现它的用处了吗?
把它放在所有浮点运算上会有什么副作用吗?
我已经查了这是做什么,但有人实际上有一个例子,当你在Java中使用strictfp关键字?有人真的发现它的用处了吗?
把它放在所有浮点运算上会有什么副作用吗?
当前回答
'strictfp'关键字用于强制Java中的浮点计算(float或double)的精度显式符合IEEE的754标准。如果不使用strictfp关键字,浮点精度取决于目标平台的硬件。
如果接口或类使用strictfp声明,那么该接口或类中的所有方法和嵌套类型都是隐式strictfp。
参考链接
其他回答
我需要它的一次(也是唯一一次)是与IBM ZSeries进行协调。在会计和大型机之外;不。有一段时间了,但我相当确定大型机没有改变。
strictfp是一个关键字,可以用作类或方法的非非访问修饰符(但不能用作变量)。将类标记为strictfp意味着类中的任何方法代码都将符合IEEE 754浮点标准规则。
如果没有这个修饰符,方法中使用的浮点可能会以依赖于平台的方式运行。有了它,无论JVM运行在哪个底层平台上,您都可以预测浮点数的行为。缺点是,如果底层平台能够支持更高的精度,那么strictfp方法将无法利用它。
如果您没有将一个类声明为strictfp,那么通过将一个方法声明为strictfp,您仍然可以在逐个方法的基础上获得strictfp行为。
~ SCJP Sun®Java™6认证程序员- Kathy Sierra & Bert Bates ~
从Java 17+开始,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.
一切都始于一个故事,
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位。