AppDividend
Latest Code Tutorials

# Java Math pow() Function Example

Java.lang.Math.pow() is an inbuilt method that is used to calculate an exponential result by taking input in terms of one double value raised to another. This method can come handy while taking into consideration any algebraic expression in a program. This method accommodates all rules of exponentiation.

## Java Math pow()

Math.pow() is used to calculate a number raised to the power of some other number. 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, which is the base, and the double value y, which is the exponent.

### Return Value

The double value 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, then the method returns the same value as the passed base value.
3. If the passed exponent value is NaN, the method returns NaN.
4. If the passed base value is NaN and the passed exponent value is non-zero, then 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, then 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, then the method returns negative infinity.

#### Example1.java: The following example demonstrates the use of this 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
```

#### Example2.java: The following example demonstrates the situation when the exponent value passed is 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```

#### Example4.java: The following example demonstrates the situation of 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
```

#### Example6.java: The following example demonstrates the case.

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```

#### Example7.java: 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
```

#### Example8.java: The following example demonstrates the case when 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
```

#### Example9.java: 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
```

#### Example10.java: 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```

#### Example11.java: 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
```

#### Example12.java: 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, then 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
```

#### Example13.java: 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```

#### Example14.java: 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, then 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```