How to use Math.pow in java?

How to do powers in java using the math class?

How to do powers in java using the math class?

Math.pow has two parameters or arguments of type double. The first a is the value that I want to raise to the power b. The method returns the value a power of type double primitive. Here below is the java guide that reports with its example. Good work.

Math.pow(double a, double b) is a static method of the Math class in Java. It takes two arguments (of type double) and raises the value of the first argument by the second argument i.e. a^b, and returns it as result (also double). In other words, a is the base and b is its exponent. For example, Math.pow(2,4) is equivalent to 2^4 or 16.

The return type Math.pow(...) is double. You can cast it to long or int (be careful of overflow for the latter.) Let’s look at some examples.

 import static java.lang.Double.NaN; public class Main { public static void main(String[] args) { System.out.println((long) Math.pow(2, 4)); // 16 System.out.println((long) Math.pow(2, 1)); // 2 System.out.println((long) Math.pow(2, 0)); // 1 // If the second argument is NaN, then the result is NaN. System.out.println(Math.pow(2, NaN)); // 0 System.out.println(Math.pow(2.5, 3)); // 15.625 } } 

Output

16 2 1 NaN 15.625