Will floating point operations on the JVM give the same results on all platforms?

后端 未结 3 1533
暖寄归人
暖寄归人 2021-01-31 07:02

I\'m using Java in an application running on multiple machines, and all machines need to get the same results for mathematical operations. Is it safe to use Java\'s floating poi

3条回答
  •  孤独总比滥情好
    2021-01-31 08:06

    Not in general, no. However, you can use strictfp expressions:

    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.

提交回复
热议问题