While programming in Java, there can be instances when one needs to evaluate a number based on the provided power. For instance, a user wants to multiply a particular number with an identical number multiple times. In such cases, the “Math.pow()” method in Java is of great aid in carrying out mathematical computations conveniently.
This write-up is all about using the Math.pow() method in Java.
How to Use the “Math.pow()” Method in Java?
The “Math.pow()” method is used to evaluate a number based on the specified power. It works in such a way that the first parameter is evaluated based on the power specified as its second parameter.
Syntax
In the above syntax:
- “n1” refers to the base.
- “n2” corresponds to the exponent.
Example 1: Utilization of “Math.pow()” Method Upon Integers
In this example, the Math.pow() method can be utilized to compute two integers such that one integer is the power of another:
int val2 = 5;
System.out.println(Math.pow(val1, val2));
According to the above code snippet:
- Initialize the two integer values.
- After that, apply the “pow()” method such that the former parameter is evaluated based on the power specified as the latter parameter.
Output
In this output, it can be seen that the specified base is computed based on the corresponding exponent.
Example 2: Utilization of “Math.pow()” Method Upon Infinity
In this particular example, the Math.pow() method can be applied to evaluate an integer by passing infinity as its power:
double infinity = Double.POSITIVE_INFINITY;
System.out.println(Math.pow(number, infinity));
In the above lines of code:
- Likewise, initialize the stated integer value.
- In the next step, “POSITIVE_INFINITY” is utilized to specify the positive infinity.
- Lastly, apply the “pow()” method by associating the specified infinity with the integer’s power.
- This will return “infinity” as a result since the power is so(infinity).
Output
That was all about using the Math.pow() method in Java.
Conclusion
The “Math.pow()” method in Java can be utilized to evaluate the specified base with respect to the specified power. This method can be used to evaluate a number based on the power specified as integer or infinity, respectively. This blog elaborated on the usage of the “Math.pow()” method in Java.