gpt4 book ai didi

java - 我什么时候应该在 java 中使用 "strictfp"关键字?

转载 作者:bug小助手 更新时间:2023-10-28 10:40:05 26 4
gpt4 key购买 nike

我已经查看了它的作用,但实际上有没有人举例说明何时在 Java 中使用 strictfp 关键字?有没有人真的发现了这个的用途?

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

最佳答案

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

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.

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

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

关于java - 我什么时候应该在 java 中使用 "strictfp"关键字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/517915/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com