The java.lang.Math.pow() is used to calculate a number raise to the power of some other number. This function accepts two parameters and returns the value of first parameter raised to the second parameter. There are some special cases as listed below:
- If the second parameter is positive or negative zero then the result will be 1.0.
- If the second parameter is 1.0 then the result will be same as that of the first parameter.
- If the second parameter is NaN then the result will also be NaN.
Syntax:
public static double pow(double a, double b) Parameter: a : this parameter is the base b : this parameter is the exponent. Return : This method returns ab.
Example 1: To show working of java.lang.Math.pow() method.
// Java program to demonstrate working // of java.lang.Math.pow() method import java.lang.Math; class Gfg { // driver code public static void main(String args[]) { double a = 30; double b = 2; System.out.println(Math.pow(a, b)); a = 3; b = 4; System.out.println(Math.pow(a, b)); a = 2; b = 6; System.out.println(Math.pow(a, b)); } } |
Output:
900.0 81.0 64.0
Example 2: To show working of java.lang.Math.pow() method when argument is NaN.
// Java program to demonstrate working // of java.lang.Math.pow() method import java.lang.Math; // importing java.lang package public class GFG { public static void main(String[] args) { double nan = Double.NaN; double result; // Here second argument is NaN, // output will be NaN result = Math.pow(2, nan); System.out.println(result); // Here second argument is zero result = Math.pow(1254, 0); System.out.println(result); // Here second argument is one result = Math.pow(5, 1); System.out.println(result); } } |
Output:
NaN 1.0 5.0
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- StrictMath pow() Method in Java
- BigInteger pow() Method in Java
- Java Guava | pow(int b, int k) method of IntMath Class
- BigDecimal pow() method in Java with Examples
- Java Guava | pow(long b, int k) of LongMath Class with Examples
- Java Math asin() method with Example
- Java Math log() method with example
- Java Math hypot() method with Example
- Java Math exp() method with Example
- Java Math getExponent() method with Example
- Java Math getExponent() method with Example
- Java Math expm1() method with Example
- Java Math round() method with Example
- Java Math nextAfter() method with Example
- Java.math.BigInteger.modInverse() method in Java
- Java.math.BigInteger.probablePrime() method in Java
- Java.lang.Math Class in Java | Set 1
- Java.lang.Math Class in Java | Set 2
- java.lang.Math.atan2() in Java
- Java Math random() method with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

