2015-11-23 26 views
4

我這就要求Math.exp(double)然後StrictMath.exp(double)和一個原因,我無法理解的結果上Java 8是不同的,儘管事實上,一個很簡單的Java測試用例從JDK源代碼看來,Math.exp只是代表StrictMath.exp。上Java 8的Java Math.exp(雙)VS StrictMath.exp(雙)

public static void main(String[] args) { 
    Properties p = System.getProperties(); 
    System.out.println(p.get("java.runtime.version")); 
    System.out.println(p.get("java.specification.version")); 
    System.out.println(Math.exp(1d)); 
    System.out.println(StrictMath.exp(1d)); 
} 

結果:

1.8.0_66-b18 
1.8 
null 
2.718281828459045 
2.7182818284590455 

Java 7

1.7.0_21-b11 
1.7 
2.7182818284590455 
2.7182818284590455 

讚賞任何指針,比任何真正的問題更多的是出於好奇。

+0

而且'Math.exp(1d)== StrictMath.exp(1d)' - >'false' – user11153

+0

http://stackoverflow.com/questions/17410559/why-does-math-sin-delegate- to-strictmath-sin也是一個很好的描述。 – Joe

回答

2

這個問題本質上是喬鏈接到問題的反面:java.lang.Math.log replaced by intrinsic call, why not java.lang.Math.exp()?

之前revision 6759698e3140Math.exp()在運行時沒有被取代的intrinsic call,這意味着它委託給StrictMath.exp()。在修訂之後(並且因此所有Java 8)有可能Math.exp()會給出不同的結果,因爲JVM會優化這些調用。

從概念上講,你可以將Math想象爲提供「快速」功能,而StrictMath提供「正確」功能。 Math回落到StrictMath時,它可以做的不好,但關於Math的行爲的保證弱於StrictMath,因此可以優化(可能以一些有限的正確性爲代價)超過StrictMath