Math.pow根据Java版本产生不同的结果 [英] Math.pow yields different result depending on java version
问题描述
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屋!