Java Math.pow() method Usage
In this quick article, We’ll see usage of Java Math.pow() method which takes two arguments, a and b, and returns a to the power of b i.e. ab
Syntax
public double pow(double a, double b)
Where,
- Parameter a is the base
- Parameter b is the exponent
- Returns ab
Example
// Returns 8.0
double result = (int) Math.pow(2, 3);
// Cast to int, returns 8
int intResult = (int) Math.pow(2, 3);
// Returns 117.29730800599916
double doubleResult = Math.pow(2.5, 5.2);
// Returns 1.0
double zeroPowerResult = Math.pow(2, 0);
// Returns 2.0
double sameResult = Math.pow(2, 1);
// Returns NaN
double nanResult = Math.pow(2, Double.NaN);
Points to note:-
- By default, returns the result in double
- You can cast the result to int
- Both arguments, base and exponent can have decimal points
- If second argument is zero then result will be 1.0
- If second argument is 1 then result will be value of first argument
- If second argument is NaN then result will be NaN