在所有平台上,JVM上的浮点运算是否会得到相同的结果? [英] Will floating point operations on the JVM give the same results on all platforms?

查看:180
本文介绍了在所有平台上,JVM上的浮点运算是否会得到相同的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一台运行在多台机器上的应用程序中使用Java,所有的机器都需要得到相同的数学运算结果。使用Java的浮点基元是否安全?还是应该使用定点数学库?

不过,您可以使用 strictfp 表达式
$ b


在FP-strict表达式中,所有的中间值必须是浮点值集合或双值集合的元素,意味着所有FP-strict表达式的结果都必须是IEEE 754算法用单一格式和双格式表示的操作数预测的结果。

在不是FP严格的表达式中,某些实现使用扩展指数范围来表示中间结果;总的来说,净效应是,在独占使用浮点值集或双值集可能导致上溢或下溢的情况下,计算可能产生正确答案。



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 point primitives? Or should I just use a fixed-point math library?

解决方案

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.

这篇关于在所有平台上,JVM上的浮点运算是否会得到相同的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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