LogIn
I don't have account.

Java Math.pow() method

DevSniper

192 Views

Math.pow() method in Java is a part of the java.lang.Math class and is used to raise a number (base) to the power of another number (exponent). It is mostly used in mathematical calculations, such as computing squares, cubes or any arbitrary power of a number.

Syntax
public static double pow(double base, double exponent) 
  • It is a static method, so it can be called directly using the class name, no need to create a reference of Math class.
  • It will always returns a double value, even if the inputs are integers.
  • If the exponent value is a negative or positive Zero this method will always return 1.0.
  • If the exponent is NaN then this method will return NaN.
  • If the exponent is 1 then this method will return first argument ( base).
  • If the base (first argument) is one ( 1 or 1.0 or 1.0f or 1L ) then this method will return 1.0
Example 1
Copy
public class Program{

     public static void main(String []args){
        System.out.println("Math.pow(1,5) : "+Math.pow(1,5));
        System.out.println("Math.pow(1.0,5) : "+Math.pow(1.0,5));
        System.out.println("Math.pow(1.0f,5) : "+Math.pow(1.0f,5));
        System.out.println("Math.pow(1L,5) : "+Math.pow(1L,5));
        System.out.println("Math.pow(41,1) : "+Math.pow(41,1));
        System.out.println("Math.pow(0,5) : "+Math.pow(0,5));
        System.out.println("Math.pow(3,0) : "+Math.pow(3,0));
        System.out.println("Math.pow(3,5) : "+Math.pow(3,5));
        System.out.println("Math.pow(3.0,5) : "+Math.pow(3.0,5));
        System.out.println("Math.pow(3,5.0) : "+Math.pow(3,5.0));
        System.out.println("Math.pow(3.2,5) : "+Math.pow(3.2,5));
        System.out.println("Math.pow(3,5.4) : "+Math.pow(3,5.4));
        System.out.println("Math.pow(3.2,5.4) : "+Math.pow(3.2,5.4));
     }
}
Math.pow(1,5) : 1.0
Math.pow(1.0,5) : 1.0
Math.pow(1.0f,5) : 1.0
Math.pow(1L,5) : 1.0
Math.pow(41,1) : 41.0
Math.pow(0,5) : 0.0
Math.pow(3,0) : 1.0
Math.pow(3,5) : 243.0
Math.pow(3.0,5) : 243.0
Math.pow(3,5.0) : 243.0
Math.pow(3.2,5) : 335.5443200000001
Math.pow(3,5.4) : 377.09847446143255
Math.pow(3.2,5.4) : 534.3303993219894
Example 2
Copy
public class Program{

     public static void main(String []args){
        System.out.println("Math.pow(1,-5) : "+Math.pow(1,-5));
        System.out.println("Math.pow(41,-1) : "+Math.pow(41,-1));
        System.out.println("Math.pow(0,-5) : "+Math.pow(0,-5));
        System.out.println("Math.pow(3,-0) : "+Math.pow(3,-0));
        System.out.println("Math.pow(3,-5) : "+Math.pow(3,-5));
        System.out.println("Math.pow(-3,5) : "+Math.pow(-3,5));
        System.out.println("Math.pow(-3,4) : "+Math.pow(-3,4));
        System.out.println("Math.pow(3.0,-5) : "+Math.pow(3.0,-5));
        System.out.println("Math.pow(3,-5.0) : "+Math.pow(3,-5.0));
        System.out.println("Math.pow(3.2,-5) : "+Math.pow(3.2,-5));
        System.out.println("Math.pow(3,-5.4) : "+Math.pow(3,-5.4));
        System.out.println("Math.pow(3.2,-5.4) : "+Math.pow(3.2,-5.4));
     }
}
Math.pow(1,-5) : 1.0
Math.pow(41,-1) : 0.024390243902439025
Math.pow(0,-5) : Infinity
Math.pow(3,-0) : 1.0
Math.pow(3,-5) : 0.00411522633744856
Math.pow(-3,5) : -243.0
Math.pow(-3,4) : 81.0
Math.pow(3.0,-5) : 0.00411522633744856
Math.pow(3,-5.0) : 0.00411522633744856
Math.pow(3.2,-5) : 0.0029802322387695304
Math.pow(3,-5.4) : 0.0026518272221286174
Math.pow(3.2,-5.4) : 0.0018715012308281498