什么时候应该使用“strictfp”在java中的关键字? [英] When should I use the "strictfp" keyword in java?

查看:146
本文介绍了什么时候应该使用“strictfp”在java中的关键字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经查过这是干什么的,但是有没有人真的有一个例子说明在Java中何时使用 strictfp 关键字?有没有人真的找到了这个用途?



是否有任何副作用只是把它放在我所有的浮点操作?
Strictfp确保你在每个平台上得到与你的浮点计算完全相同的结果。如果你不使用strictfp,那么JVM实现可以在可用的地方使用额外的精度。






p>在FP-strict表达式中,所有
中间值必须是浮点值集合的元素
或者双
值集合,这意味着所有FP-严格表达式必须是
那些由IEEE 754算法
预测的操作数,用
和double格式表示。在不是FP-strict的
表达式中,为
实现授予一些
的余地,使用一个扩展的
指数范围来表示
的中间结果;净效应
粗略地说就是
的计算可能会产生正确的
答案,在这种情况下,浮动值集b $ b或
值集可能会导致溢出或
下溢。

换句话说,就是要确保 一次运行 - 任何地方实际上意味着<一次写入一次 - 获取 - 相等 - 错误 - 结果 - 任何地方



With strictfp your结果是可移植的,没有它,他们更可能是准确的。


I've looked up what this does, but does anyone actually have an example of when you would use the strictfp keyword in Java? Has anyone actually found a use for this?

Would there be any side-effects of just putting it on all my floating point operations?

解决方案

Strictfp ensures that you get exactly the same results from your floating point calculations on every platform. If you don't use strictfp, the JVM implementation is free to use extra precision where available.

From the 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.

In other words, it's about making sure that Write-Once-Run-Anywhere actually means Write-Once-Get-Equally-Wrong-Results-Everywhere.

With strictfp your results are portable, without it they are more likely to be accurate.

这篇关于什么时候应该使用“strictfp”在java中的关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆