Java pow Function

The Java pow Function is one of the Math Library functions used to calculate the Power of the specified expression.

Java pow Function syntax

The syntax of the Math.pow in Java Programming language is

static double pow(double base, double Exponent); // Return Type is Double

//In order to use in program use following code
Math.pow(double base, double Exponent);
  • Base: Please specify the base value here.
  • Exponent: Please specify the Exponent value or power here.

For example, if x is base value and 2 is exponent then, Math.pow (x, 2) = x².

The Java Math.pow function returns the Base value raised to the power of the Exponent value. There are some special cases where the pow Function result will be different, and they are:

  • If the Exponent argument is positive or negative zero, it will return the result as 1.0.
  • If either the Base value or Exponent value argument is not a Number, Java Math.pow will return the result as NaN.
  • When the Absolute value of the Base value is equal to one, and the Exponent value is infinity, then it returns the result as NaN.
  • If
    • the Absolute value of a Base value is greater than 1, and an Exponent value is positive infinity, or
    • The absolute of the Base value is less than one, and if the Exponent value is negative infinity, then the Java math pow function result is Positive Infinity.
  • If
    • the Absolute value of the Base value is greater than 1, and the Exponent value is negative infinity, or
    • The absolute value of the Base value is less than 1, and if an Exponent value is a positive infinity, then the result is Positive Zero.
  • When
    • the Base exponent is positive zero & the Exponent value is greater than zero, or
    • the Base value is positive infinity & the Exponent value is less than zero, then the result is Positive Zero.
  • If
    • the Base value is positive zero, and the Exponent value is less than zero, or
    • If the base value is positive infinity & the Exponent value is greater than zero, then the result is Positive Infinity.
  • If
    • the Base value is negative zero & the Exponent value is greater than zero but not a finite odd integer, or
    • The base argument is a negative infinity & the Exponent value is less than zero but not a finite odd integer. The Java Math.pow returns the result is Positive Zero.
  • When
    • the Base value is negative zero & Exponent value is a positive finite odd integer, or
    • the Base value is negative infinity, and an Exponent value is a negative finite odd integer, then the result is Negative Zero.
  • If
    • the Base value is negative zero, and the Exponent value is less than zero but not a finite odd integer, or
    • The base value is negative infinity, and if the Exponent value is greater than zero but not a finite odd integer, then the result is Positive Infinity.
  • If
    • the Base value of the Java math pow function is negative zero, and the Exponent value is a negative finite odd integer, or
    • the Base value is negative infinity, and the Exponent value is a positive finite odd integer. It will return the result is Negative Infinity.
  • The Base value of math pow is finite & less than zero and
    • if the Exponent value is a finite even integer, the result is equal to the result of raising the absolute value of the Base value to the power of the Exponent value.
    • The exponent value of math pow java is a finite odd integer. The result is equal to the negative of the result of raising the absolute value of a Base value to the power of the Exponent value.
    • If the Exponent value is finite and not an integer. Then it will return the result is NaN.

Java pow Function Example

The Java Math.pow function returns the Power of the given number. In this example, we will find the power of both positive and negative double values and display the output.

package MathFunctions;

public class PowMethod {
	public static void main(String[] args) {
		double a = Math.pow((5.12 - 4.65 + 3.53), 3);
		System.out.println("Math.Expm1 Result = = " + a);	
		
		System.out.println("\nMath.pow Result for Zero as Second argument = " + Math.pow(2, 0));
		System.out.println("Math.pow Result for Zero as First argument = " + Math.pow(0, 2));

		System.out.println("\nMath.pow Result of Positive Number = " + Math.pow(2, 3));
		System.out.println("Math.pow Result of Positive Number = " + Math.pow(4, 2));
		
		System.out.println("\nMath.pow Result of Negative Number = " + Math.pow(-2, 3));
		System.out.println("Math.pow Result of Negative Number = " + Math.pow(3, -4));
	}
}
Java pow Function 1

First, We declared variable a of type Double. Next, we used the Java Math.pow function directly on expression. Here, we used System.out.println statement to print the result as output.

double a = Math.pow((5.12 - 4.65 + 3.53), 3);
System.out.println("Math.Expm1 Result = = " + a);

From the above statement, Math.pow((5.12 – 4.65 + 3.53), 3) ==> Math.pow(4, 3) ==> 64. Next, We used the Math.pow with one positive double value and another argument as zero.

System.out.println("\nMath.pow Result for Zero as Second argument = " + Math.pow(2, 0));
System.out.println("Math.pow Result for Zero as First argument = " + Math.pow(0, 2));

Here, We used the Java Math power Function directly on Positive double values.

System.out.println("\nMath.pow Result of Positive Number = " + Math.pow(2, 3));
System.out.println("Math.pow Result of Positive Number = " + Math.pow(4, 2));

Next, We used the Math function directly on Negative double values inside our print line statement.

System.out.println("\nMath.pow Result of Negative Number = " + Math.pow(-2, 3));
System.out.println("Math.pow Result of Negative Number = " + Math.pow(3, -4));

Comments are closed.