Math.pow根据Java版本产生不同的结果 [英] Math.pow yields different result depending on java version

查看:314
本文介绍了Math.pow根据Java版本产生不同的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JDK版本1.7.0_60上运行以下代码:

  System.out.println(Math。 pow(1.5476348320352065,(0.3333333333333333))); 

结果是:1.1567055833133086

'm在JDK版本1.7.0上运行完全相同的代码。



结果是:1.1567055833133089

我知道double并不是无限精确的,但是在java规范中是否有一个改变,导致了这个区别?



PS:因为我们使用一个遗留系统,所以Big Decimal不是一个选项。编辑:我能够追踪更改的时间:它是在JDK版本1.7.0_40(与1.7版相比)中引入的。为了在所有Java版本之间产生一致的结果,解决方案是使用 StrictMath。 ()而不是 Math.pow()



什么可能会导致差异,请参阅这个答案


I'm running the following code on a JDK Version 1.7.0_60:

System.out.println(Math.pow(1.5476348320352065, (0.3333333333333333)));

The result is: 1.1567055833133086

I'm running exactly the same code on a JDK Version 1.7.0.

The result is: 1.1567055833133089

I understand that double is not infinitely precise, but was there a change in the java spec that causes the difference?

PS: Because we use a legacy system, Big Decimal is not an option.

Edit: I was able to track down the time of the change: It was introduced in the JDK Version 1.7.0_40 (as compared to Version 1.7.0_25).

解决方案

To produce consistent results between all Java versions, the solution was to use StrictMath.pow() instead of Math.pow().

For background information on what might cause the difference, refer to this answer.

这篇关于Math.pow根据Java版本产生不同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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