Home Il Progetto Piani Mission
Accedi Registrati
Home

Come usare Math.pow in java?

Come usare Math.pow in java?

Come fare le potenze in java utilizzando la classe math?

Math.pow ha due parametri o argomenti di tipo double. Il primo a รจ il valore che voglio elevare alla potenza b. Il metodo ci restituisce il valore a potenza di tipo double primitivo. Ecco sotto la guida java cosa riporta con il suo esempio. Buon lavoro.

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