AppDividend
Latest Code Tutorials

# Java Math.pow Function: The Complete Guide

Math.pow() is used to calculate a number raised to the power of some other number. The pow() method comes in handy while taking into consideration any algebraic expression in a program. In addition, this method accommodates all rules of exponentiation.

## Java Math.pow

Java Math.pow() is a built-in method used to calculate an exponential result by taking input in terms of one double value raised to another. The Math.pow() function accepts two parameters and returns the value of the first parameter raised to the second parameter.

### Syntax

```public static double pow(double x, double y)
```

### Parameter(s)

The double value x is the base, and the double value y, which is the exponent.

### Return Value

The double value is x to the power of y.

#### Note

1. If the passed exponent value is positive zero or negative zero, then the method returns 1.0
2. If the passed exponent value is 1.0, the method returns the same value as the given base value.
3. If the passed exponent value is NaN, the method returns NaN.
4. If the passed base value is NaN and the given exponent value is non-zero, the method returns NaN. If the exponent value is zero, then the method returns 1.0
5. If the absolute value of the base is greater than 1.0 and the exponent is positive infinity, or if the absolute value of the base is less than 1.0 and the exponent is negative infinity, this method returns positive infinity.
6. If the absolute value of the base is greater than 1.0 and the exponent is negative infinity, or if the absolute value of the base is less than 1.0 and the exponent is positive infinity, this method returns positive zero.
7. If the absolute value of the base is 1.0 and the exponent is infinite, then the method returns NaN.
8. If the base value is positive, zero, and the exponent is positive, or if the base value is positive infinity and the exponent is negative, the method returns positive zero.
9. If the base value is positive, zero, and the exponent is negative, or if the base value is positive infinity and the exponent is positive, the method returns positive infinity.
10. If the base value is negative, zero, and the exponent value is positive but not a finite odd integer, or if the base value is negative infinity and the exponent value is negative but not a finite odd integer, then the method returns positive zero.
11. If the base value is negative zero and the exponent value is a positive finite odd integer, or if the base value is negative infinity and the exponent value is a negative finite odd integer, the method returns negative zero.
12. If the base value is negative zero and the exponent value is negative but not a finite odd integer, or if the base value is negative infinity and the exponent value is positive but not a finite odd integer, then the method returns positive infinity.
13. If the base value is negative zero and the exponent value is a negative finite odd integer, or if the base value is negative infinity and the exponent value is a positive finite odd integer, the method returns negative infinity.

## Demonstrates the use of Java Math.pow() method.

```import java.util.*;

public class Example1 {
public static void main(String[] args) {
double x = 2.0;
double y = 4.0;
System.out.println(Math.pow(x, y));
}
}
```

#### Output

```->javac Example1.java
->java Example1
16.0
```

## Passing the exponent value either positive or negative zero

```import java.util.*;

public class Example2 {
public static void main(String[] args) {
double x = 2.0;
double y = 0.0;
double z = -0.0;
System.out.println(Math.pow(x, y));
System.out.println(Math.pow(x, z));
}
}
```

#### Output

```->javac Example2.java
->java Example2
1.0
1.0
```

#### Example3.java: The following example demonstrates the situation of passing exponent value as 1.0

```import java.util.*;

public class Example3 {
public static void main(String[] args) {
double x = 2.0;
double y = 1.0;
System.out.println(Math.pow(x, y));
}
}
```

#### Output

```->javac Example3.java
->java Example3
2.0```

## Passing exponent value as NaN.

```import java.util.*;

public class Example4 {
public static void main(String[] args) {
double x = 2.0;
double y = Double.NaN;
System.out.println(Math.pow(x, y));
}
}
```

#### Output

```->javac Example4.java
->java Example4
NaN
```

#### Example5.java: The following example demonstrates the situation of passing the base value as NaN.

```import java.util.*;

public class Example5 {
public static void main(String[] args) {
double x = Double.NaN;
double y = 0.0;
double z = 2.0;

System.out.println(Math.pow(x, y));
System.out.println(Math.pow(x, z));
}
}
```

#### Output

```->javac Example5.java
->java Example5
1.0
NaN
```

If the absolute value of the base is greater than 1.0 and the exponent is positive infinity, or if the absolute value of the base is less than 1.0 and the exponent is negative infinity, this method returns positive infinity.

See the following code.

```import java.util.*;

public class Example6 {
public static void main(String[] args) {
double x = 2.0;
double y = Double.POSITIVE_INFINITY;

double a = 0.3;
double b = Double.NEGATIVE_INFINITY;

System.out.println(Math.pow(x, y));
System.out.println(Math.pow(a, b));
}
}
```

#### Output

```->javac Example6.java
->java Example6
Infinity
Infinity```

#### The following example demonstrates the case.

If the absolute value of the base is greater than 1.0 and the exponent is negative infinity, or if the absolute value of the base is less than 1.0 and the exponent is positive infinity, this method returns positive zero.

See the following code.

```import java.util.*;

public class Example7 {
public static void main(String[] args) {
double x = 2.0;
double y = Double.NEGATIVE_INFINITY;

double a = 0.3;
double b = Double.POSITIVE_INFINITY;

System.out.println(Math.pow(x, y));
System.out.println(Math.pow(a, b));
}
}
```

#### Output

```->javac Example7.java
->java Example7
0.0
0.0
```

## Passing the absolute value of the base 1.0 and the exponent is infinite

See the following code.

```import java.util.*;

public class Example8 {
public static void main(String[] args) {
double x = 1.0;
double y = Double.POSITIVE_INFINITY;

double a = 1.0;
double b = Double.NEGATIVE_INFINITY;

System.out.println(Math.pow(x, y));
System.out.println(Math.pow(a, b));

System.out.println(Math.pow(-x, y));
System.out.println(Math.pow(-a, b));
}
}
```

#### Output

```->javac Example8.java
->java Example8
NaN
NaN
NaN
NaN
```

#### The following example demonstrates the case.

If the base value is positive, zero, and the exponent is positive, or if the base value is positive infinity and the exponent is negative, the method returns positive zero.

See the following code.

```import java.util.*;

public class Example9 {
public static void main(String[] args) {
double x = 0.0;
double y = 2.0;

double a = Double.POSITIVE_INFINITY;
double b = -2.0;

System.out.println(Math.pow(x, y));
System.out.println(Math.pow(a, b));

}
}
```

#### Output

```->javac Example9.java
->java Example9
0.0
0.0
```

#### The following example demonstrates the case.

If the base value is positive, zero, and the exponent is negative, or if the base value is positive infinity and the exponent is positive, the method returns positive infinity.

```import java.util.*;

public class Example10 {
public static void main(String[] args) {
double x = 0.0;
double y = -2.0;

double a = Double.POSITIVE_INFINITY;
double b = 2.0;

System.out.println(Math.pow(x, y));
System.out.println(Math.pow(a, b));
}
}
```

#### Output

```->javac Example10.java
->java Example10
Infinity
Infinity```

#### The following example demonstrates the case.

If the base value is negative, zero, and the exponent value is positive but not a finite odd integer, or if the base value is negative infinity and the exponent value is negative but not a finite odd integer, then the method returns positive zero.

See the following code.

```import java.util.*;

public class Example11 {
public static void main(String[] args) {
double x = -0.0;
double y = 2.0;

double a = Double.NEGATIVE_INFINITY;
double b = -2.0;

System.out.println(Math.pow(x, y));
System.out.println(Math.pow(a, b));

}
}
```

#### Output

```->javac Example11.java
->java Example11
0.0
0.0
```

#### The following example demonstrates the case.

If the base value is negative zero and the exponent value is a positive finite odd integer, or if the base value is negative infinity and the exponent value is a negative finite odd integer, the method returns negative zero.

```import java.util.*;

public class Example12 {
public static void main(String[] args) {
double x = -0.0;
double y = 3.0;

double a = Double.NEGATIVE_INFINITY;
double b = -3.0;

System.out.println(Math.pow(x, y));
System.out.println(Math.pow(a, b));

}
}
```

#### Output

```->javac Example12.java
->java Example12
-0.0
-0.0
```

#### The following example demonstrates the case.

If the base value is negative zero and the exponent value is negative but not a finite odd integer, or if the base value is negative infinity and the exponent value is positive but not a finite odd integer, then the method returns positive infinity.

See the following code.

```import java.util.*;

public class Example13 {
public static void main(String[] args) {
double x = -0.0;
double y = -2.0;

double a = Double.NEGATIVE_INFINITY;
double b = 2.0;

System.out.println(Math.pow(x, y));
System.out.println(Math.pow(a, b));

}
}
```

#### Output

```->javac Example13.java
->java Example13
Infinity
Infinity```

#### The following example demonstrates the case.

If the base value is negative zero and the exponent value is a negative finite odd integer, or if the base value is negative infinity and the exponent value is a positive finite odd integer, the method returns negative infinity.

```import java.util.*;

public class Example14 {
public static void main(String[] args) {
double x = -0.0;
double y = -3.0;

double a = Double.NEGATIVE_INFINITY;
double b = 3.0;

System.out.println(Math.pow(x, y));
System.out.println(Math.pow(a, b));

}
}
```

#### Output

```->javac Example14.java
->java Example14
-Infinity
-Infinity```

That’s it for Java Math.pow function tutorial.