![]() |
VOOZH | about |
The Math.pow() method in Java is used to calculate a number raised to the power of some other number. It is part of the java.lang.Math class and is widely used in mathematical computations, scientific calculations, and algorithmic problems. This function accepts two parameters and returns the value of first parameter raised to the second parameter.
Example 1: This example demonstrates how to use the Math.pow() method in Java to calculate the power of a number (base raised to the exponent).
900.0 81.0 556.9113382296638
Explanation: In the above code, we have declared two variables a (base) and b (exponent) and then the program calculates the result of raising the base (a) to the power of the exponent (b) using the Math.pow() method and prints the result.
The method signature is listed below:
public static double pow(double base, double exponent)
Parameter:
Return Types: This method return a floating-point number (e.g., 8.0, 2.5, NaN).
The below table demonstrates the special cases:
Cases | Result |
|---|---|
exponent = 0 | 1.0 (any number⁰ = 1) |
exponent = 1 | Same as base (e.g., 5¹ = 5) |
exponent = NaN | NaN (Not a Number) |
base = 0, exponent < 0 | Infinity |
base = NaN | NaN |
Important Points:
- Any number raised to the power of 0 equals 1. This is a fundamental rule in mathematics, known as the "zero exponent rule." For example, 5⁰ = 1, 10⁰ = 1, etc.
- Any number raised to the power of 1 is the number itself. This is because raising a number to the power of 1 does not change its value. For example, 5¹ = 5, 10¹ = 10, etc.
- If the exponent is "NaN" (Not a Number), the result of the power operation becomes "NaN" as well.
- When the base is 0 and the exponent is a negative number (e.g., 0 raised to a negative power), it results in infinity.
- If the base is "NaN" (Not a Number), any power of it also results in "NaN."
Example 2: This example demonstrates how the Math.pow() method handles special cases, such as NaN, 0, and 1 as exponents.
NaN 1.0 5.0
Explanation: The above code handles special cases in exponentiation such as When the exponent is NaN, the result is NaN, When the exponent is 0, the result is 1.0 (except for base 0), When the exponent is 1, the result is the base itself.