今天遇到一個prod issue,計算PPL的時候結果爲NaN,順着找代碼,發現Math.pow還挺有意思,就把case都理了一遍,記錄下。
java
先上JDK Doc:app
pow public static double pow(double a, double b) Returns the value of the first argument raised to the power of the second argument. Special cases: (In the foregoing descriptions, a floating-point value is considered to be an integer if and only if it is finite and a fixed point of the method ceil or, equivalently, a fixed point of the method floor. A value is a fixed point of a one-argument method if and only if the result of applying the method to the value is equal to the value.) The computed result must be within 1 ulp of the exact result. Results must be semi-monotonic. Parameters: a - the base. b - the exponent. Returns: the value ab. if the second argument is a finite even integer, the result is equal to the result of raising the absolute value of the first argument to the power of the second argument if the second argument is a finite odd integer, the result is equal to the negative of the result of raising the absolute value of the first argument to the power of the second argument if the second argument is finite and not an integer, then the result is NaN. the first argument is negative zero and the second argument is a negative finite odd integer, or the first argument is negative infinity and the second argument is a positive finite odd integer, the first argument is negative zero and the second argument is less than zero but not a finite odd integer, or the first argument is negative infinity and the second argument is greater than zero but not a finite odd integer, the first argument is negative zero and the second argument is a positive finite odd integer, or the first argument is negative infinity and the second argument is a negative finite odd integer, the first argument is negative zero and the second argument is greater than zero but not a finite odd integer, or the first argument is negative infinity and the second argument is less than zero but not a finite odd integer, the first argument is positive zero and the second argument is less than zero, or the first argument is positive infinity and the second argument is greater than zero, the first argument is positive zero and the second argument is greater than zero, or the first argument is positive infinity and the second argument is less than zero, the absolute value of the first argument is greater than 1 and the second argument is negative infinity, or the absolute value of the first argument is less than 1 and the second argument is positive infinity, the absolute value of the first argument is greater than 1 and the second argument is positive infinity, or the absolute value of the first argument is less than 1 and the second argument is negative infinity, If the second argument is positive or negative zero, then the result is 1.0. If the second argument is 1.0, then the result is the same as the first argument. If the second argument is NaN, then the result is NaN. If the first argument is NaN and the second argument is nonzero, then the result is NaN. Ifthen the result is positive infinity. Ifthen the result is positive zero. If the absolute value of the first argument equals 1 and the second argument is infinite, then the result is NaN. Ifthen the result is positive zero. Ifthen the result is positive infinity. Ifthen the result is positive zero. Ifthen the result is negative zero. Ifthen the result is positive infinity. Ifthen the result is negative infinity. If the first argument is finite and less than zero If both arguments are integers, then the result is exactly equal to the mathematical result of raising the first argument to the power of the second argument if that result can in fact be represented exactly as a double value.
再上中文釋義:less
pow public static double pow(double a, double b) 返回第一個參數的第二個參數次冪的值。特殊狀況以下: 若是第二個參數爲正 0 或負 0,那麼結果爲 1.0。 若是第二個參數爲 1.0,那麼結果與第一個參數相同。 若是第二個參數爲 NaN,那麼結果爲 NaN。 若是第一個參數爲 NaN,第二個參數非 0,那麼結果爲 NaN。 若是那麼結果爲正無窮大。 第一個參數的絕對值大於 1,而且第二個參數爲正無窮大,或者 第一個參數的絕對值小於 1,而且第二個參數爲負無窮大, 若是那麼結果爲正 0。 第一個參數的絕對值大於 1,而且第二個參數爲負無窮大,或者 第一個參數的絕對值小於 1,而且第二個參數爲正無窮大, 若是第一個參數的絕對值等於1,而且第二個參數爲無窮大,那麼結果爲 NaN。 若是那麼結果爲正 0。 第一個參數爲正 0,而且第二個參數大於 0,或者 第一個參數爲正無窮大,而且第二個參數小於 0, 若是那麼結果爲正無窮大。 第一個參數爲正 0,而且第二個參數小於 0,或者 第一個參數爲正無窮大,而且第二個參數大於 0, 若是那麼結果爲正 0。 若是第一個參數爲負 0,而且第二個參數大於 0 但不是有限的奇數整數,或者 第一個參數爲負無窮大,而且第二個參數小於 0 但不是有限的奇數整數, 若是那麼結果爲負 0。 第一個參數爲負 0,而且第二個參數爲正的有限奇數整數,或者 第一個參數爲負無窮大,而且第二個參數爲負的有限奇數整數, 若是那麼結果爲正無窮大。 若是第一個參數爲負 0,而且第二個參數小於 0 但不是有限的奇數整數,或者 第一個參數爲負無窮大,而且第二個參數大於 0 但不是有限的奇數整數, 若是那麼結果爲負無窮大。 第一個參數爲負 0,而且第二個參數爲負的有限奇數整數,或者 第一個參數爲負無窮大,而且第二個參數爲正的有限奇數整數, 若是第一個參數爲小於 0 的有限值, 若是第二個參數爲有限的偶數整數,那麼結果等於第一個參數絕對值的第二個參數次冪的結果。 若是第二個參數爲有限的奇數整數,那麼結果等於負的第一個參數絕對值的第二個參數次冪的結果。 若是第二個參數爲有限的非整數值,那麼結果爲 NaN。 若是兩個參數都爲整數,而且結果剛好能夠表示爲一個 double 值,那麼該結果剛好等於第一個參數的第二個參數次冪的算術結果。 (在前面的描述中,當且僅當浮點數爲有限值而且是方法 ceil 的定點數,或者是方法 floor 的定點數時,才能夠認爲浮點值是整數。當且僅當將某個單參數方法應用到某個值的結果等於該值時,該值纔是這個方法的定點值。) 計算結果必須在準確結果的 1 ulp 範圍內。結果必須具備半單調性。 參數: a - 底數。 b - 指數。 返回: 值 ab。
計算實例太長,下一篇顯示ide