In this article, we’ll go through Math.pow() method in Java and how it works.
1. What is Math.pow in Java
Math.pow
method of java.lang
package is used to calculate the ax
provided you pass a base(a
) and an exponent(x
) as parameters. The signature of Math.pow
is the following:
public static double pow(double a, double b)
Math.pow
always returns a double
. Additionally, double
a
is the base and double
b
is the exponent.
2. How to use Math.pow in Java
To use math.pow, you just call the method with a double number as a base and another double number as the exponent.
2.1 Base is Negative
Math.pow(-5, -2); // 0.04 Math.pow(-5, 0); // 1.0 Math.pow(-5, 0.5); // NaN since -5^0.5 = √-5 which can not be computed for real numbers Math.pow(-5, 1); // -5.0 Math.pow(-5, 100);// 7.888609052210118E69 Math.pow(-5, Double.NaN); // NaN Math.pow(-5, Double.NEGATIVE_INFINITY); // 0.0 Math.pow(-5, Double.POSITIVE_INFINITY); // Infinity
Here we should note that when the second argument is NaN
, the result is always NaN
.
2.2 Base is Zero
Math.pow(0, -2); //Infinity Math.pow(0, 0); //1.0 Math.pow(0, 0.5); //0.0 Math.pow(0, 1); //0.0 Math.pow(0, 100); //0.0 Math.pow(0, Double.NaN); //NaN Math.pow(0, Double.NEGATIVE_INFINITY); //Infinity Math.pow(0, Double.POSITIVE_INFINITY); // 0.0
While 00 is undefined in mathematics, in Java, the returned result is 1.
2.3 Base is between Zero and One Exclusively
Math.pow(0.2, -2); //24.999999999999996 Math.pow(0.5, 0); //1.0 Math.pow(0.5, 0.5); //0.7071067811865476 Math.pow(0.5 , 1); //0.5 Math.pow(0.5 , 100); //7.888609052210118E-31 Math.pow(0.5 , Double.NaN); // NaN Math.pow(0.6 , Double.NEGATIVE_INFINITY); //Infinity Math.pow(0.5 , Double.POSITIVE_INFINITY); //0.0
2.4 Base Is Greater Than One
Math.pow(6, -2); //0.027777777777777776 Math.pow(4, 0); //1.0 Math.pow(100.5, 0.5); //10.024968827881711 Math.pow(4 , 1); //4.0 Math.pow(4 , 100); //1.6069380442589903E60 Math.pow(4 , Double.NaN); //NaN Math.pow(4 , Double.NEGATIVE_INFINITY); //0.0 Math.pow(4 , Double.POSITIVE_INFINITY); //Infinity
2.5 Base is NaN
NaN means “Not a Number” and it is used in Java and generally in programming to represent some undefined values.
Math.pow(Double.NaN, -3); //NaN Math.pow(Double.NaN, -2); //NaN Math.pow(Double.NaN, 0); //1.0 Math.pow(Double.NaN, 0.5); //NaN Math.pow(Double.NaN , 1); //NaN Math.pow(Double.NaN , 100); //NaN Math.pow(Double.NaN , Double.NaN); //NaN Math.pow(Double.NaN , Double.NEGATIVE_INFINITY); //NaN Math.pow(Double.NaN , Double.POSITIVE_INFINITY); //NaN
2.6 Base Is -Infinity
Negative infinity is defined as the calculation of –1.0/0
in Double class.
Math.pow(Double.NEGATIVE_INFINITY, -2); //0.0 Math.pow(Double.NEGATIVE_INFINITY, 0); //1.0 Math.pow(Double.NEGATIVE_INFINITY, 0.5); //Infinity Math.pow(Double.NEGATIVE_INFINITY , 1); //-Infinity Math.pow(Double.NEGATIVE_INFINITY , 100); //Infinity Math.pow(Double.NEGATIVE_INFINITY , Double.NaN); //NaN Math.pow(Double.NEGATIVE_INFINITY , Double.NEGATIVE_INFINITY); //0.0 Math.pow(Double.NEGATIVE_INFINITY , Double.POSITIVE_INFINITY); //Infinity
2.7 Base is +infinity
Positive infinity is defined as the calculation of +1.0/0
in Double class.
Math.pow(Double.POSITIVE_INFINITY, -3); //0.0 Math.pow(Double.POSITIVE_INFINITY, -2); //0.0 Math.pow(Double.POSITIVE_INFINITY, 0); //1.0 Math.pow(Double.POSITIVE_INFINITY, 0.5); //Infinity Math.pow(Double.POSITIVE_INFINITY , 1); //Infinity Math.pow(Double.POSITIVE_INFINITY , 100); //Infinity Math.pow(Double.POSITIVE_INFINITY , Double.NaN); //NaN Math.pow(Double.POSITIVE_INFINITY , Double.NEGATIVE_INFINITY); //0.0 Math.pow(Double.POSITIVE_INFINITY , Double.POSITIVE_INFINITY); //Infinity
3. How to Do Exponents in Java Without Math.pow
If for any reason you do not want to use Math.pow
, you can create your own method:
private static double calculatePower(double base, int exponent){ double result = 1; int abs = Math.abs(exponent); for(int i = 0; i < abs; i++){ result *= base; } return exponent >= 0 ? result : 1/result; }
Note that the above method has not taken into account special cases for NaN
, -Infinity
, +Infinity
and it only works with integer exponents. Should we like to cover all the cases, we would need to write a much more complex method than that(like Math.pow
of Math class). Now let’s see our custom calculatePower
function in action:
calculatePower(2.5, 2); //6.25 calculatePower(-5, 2); //25 calculatePower(0, 2); //0.0 calculatePower(5, 0); //1.0 calculatePower(-5, 0); //1.0 calculatePower(1000, 2); //1000000.0 calculatePower(-2.5, 3); //-15.625 calculatePower(4, -1); //0.25 calculatePower(64, -1); //0.015625
4. Conclusion
By now, you should be able to use Math.pow while knowing the expected result for all special cases. You can find the source code on our GitHub page. You can also check Math.random() in Java to learn how you can generate random numbers using Math class.