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

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

问题描述

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

将它放在我所有的浮点运算上会不会有任何副作用?

解决方案

Strictfp 确保您在每个平台上从浮点计算中获得完全相同的结果.如果您不使用 strictfp,则 JVM 实现可以在可用的情况下自由使用额外的精度.

来自 JLS:><块引用>

在一个 FP-strict 表达式中,所有中间值必须是元素浮点值集或双精度值集,暗示结果的所有 FP-strict 表达式必须是由 IEEE 754 算术预测的那些在使用 single 表示的操作数上和双格式.在一个不是 FP-strict 的表达式,一些给予回旋余地使用扩展的实现要表示的指数范围中间结果;净效应,粗略地说,是一个计算可能会产生正确的回答"在排他性的情况下使用浮点值集或双精度值设置的值可能会导致溢出或下溢.

换句话说,就是确保Write-Once-Run-Anywhere实际上意味着Write-Once-Get-Equally-Wrong-Results-Everywhere.

使用 strictfp,您的结果是可移植的,没有它,它们更有可能是准确的.

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天全站免登陆