java.lang.Math.pow 释义

今天遇到一个prod issue,计算PPL的时候结果为NaN,顺着找代码,发现Math.pow还挺有意思,就把case都理了一遍,记录下。

先上JDK Doc:

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.

再上中文释义:

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。

计算实例太长,下一篇显示





你可能感兴趣的:(java.lang.Math.pow 释义)