Java Math pow() Method
The pow() method of Math class returns the value of first argument raised to the power of second argument i.e. ab.
Syntax:
public static double pow(double a, double b)
Parameters:
The parameters ‘a’ and ‘b’ represent the base and the exponent.
Return Value:
The pow() method computes the value of ab.
Special cases of the pow() method are as follows:
- It returns 1.0, is second argument passed is positive or negative zero.
- It returns the result same as the first argument, if the second argument is 1.0.
- It returns NaN, is either argument is NaN.
- It returns positive infinity, if ‘a’ is greater than 1 and ‘b’ is positive infinity or ‘a’ is less than 1 and ‘b’ is negative infinity.
- It returns positive zero, if ‘a’ is greater than 1 and ‘b’ is negative infinity or ‘a’ is less than 1 and ‘b’ is positive infinity.
- It returns NaN, if absolute value of the first argument is equal to 1 and second argument is infinite.
- It returns positive zero, if ‘a’ is positive zero and ‘b’ is greater than zero or ‘a’ is positive infinity and ‘b’ is less than zero.
- It returns positive infinity, if ‘a’ is positive zero and ‘b’ is less than zero or ‘a’ is positive infinity and ‘b’ is greater than zero.
- It returns positive zero, if ‘a’ is negative zero and ‘b’ is greater than zero but not a finite odd integer or ‘a’ is negative infinity and ‘b’ is less than zero but not a finite odd integer.
- It returns negative zero, if ‘a’ is negative zero and ‘b’ is positive finite odd integer or ‘a’ is negative infinity and ‘b’ is negative finite odd integer.
- It returns positive infinity, if ‘a’ is negative zero and ‘b’ is less than zero but not a finite odd integer or ‘a’ is negative infinity and ‘b’ is greater than zero but not a finite odd integer.
- It returns negative infinity, if ‘a’ is negative zero and ‘b’ is negative finite odd integer or ‘a’ is negative infinity and ‘b’ is positive finite odd integer.
- It returns ab, if both the arguments are integers.
Example 1:
public class JavaMathPowExample1 {
public static void main(String[] args) {
double a = 2;
double b = 8;
// computes the value of a raised to the power b
System.out.println(Math.pow(a,b));
}
}
Output:
4.0
Example 2:
public class JavaMathPowExample2 {
public static void main(String[] args) {
double a = 2;
double b = -0;
// returns 1.0, is second argument passed is positive or negative zero.
System.out.println(Math.pow(a,b));
}
}
Output:
1.0
Example 3:
public class JavaMathPowExample3 {
public static void main(String[] args) {
double a = 2;
double b = 1;
// returns the result same as the first argument, if the second argument is 1.0
System.out.println(Math.pow(a,b));
}
}
Output:
2.0
Example 4:
public class JavaMathPowExample4 {
public static void main(String[] args) {
double a = Double.POSITIVE_INFINITY;
double b = -1;
/* returns positive zero, if ‘a’ is positive zero and ‘b’ is greater than zero
or ‘a’ is positive infinity and ‘b’ is less than zero*/
System.out.println(Math.pow(a,b));
}
}
Output:
0.0
Example 5:
public class JavaMathPowExample5 {
public static void main(String[] args) {
double a = Double.NEGATIVE_INFINITY;
double b = -77;
/* returns negative zero, if ‘a’ is negative zero and ‘b’ is positive finite odd integer or
‘a’ is negative infinity and ‘b’ is negative finite odd integer*/
System.out.println(Math.pow(a,b));
}
}
Output:
-0.0
