Guayaba de Java | Método IntMath.checkedPow(int b, int k) con ejemplos

checkedPow(int b, int k) es un método de IntMath Class de Guava que acepta dos parámetros b y k y se usa para encontrar la k-ésima potencia de b.

Sintaxis:

public static int checkedPow(int b, int k)

Parámetros: El método acepta dos parámetros, b y k. El parámetro b se llama base y está elevado a la k-ésima potencia.

Valor devuelto: este método devuelve la k-ésima potencia de b.

Excepciones: El métodocheckPow(int b, int k) lanza ArithmeticException si la k-ésima potencia de b se desborda en la aritmética int con signo.

Los siguientes ejemplos ilustran la implementación del método anterior:

Ejemplo 1:

// Java code to show implementation of
// checkedPow(int b, int k) method of
// Guava's IntMath class
  
import java.math.RoundingMode;
import com.google.common.math.IntMath;
  
class GFG {
  
    // Driver code
    public static void main(String args[])
    {
        int b1 = 5;
        int k1 = 7;
  
        // Using checkedPow(int b, int k) method
        // of Guava's IntMath class
        int ans1 = IntMath.checkedPow(b1, k1);
  
        System.out.println(b1 + " to the "
                           + k1 + "th power is: "
                           + ans1);
  
        int b2 = 19;
        int k2 = 4;
  
        // Using checkedPow(int b, int k) method
        // of Guava's IntMath class
        int ans2 = IntMath.checkedPow(b2, k2);
  
        System.out.println(b2 + " to the " + k2
                           + "th power is: "
                           + ans2);
    }
}
Producción:

5 to the 7th power is: 78125
19 to the 4th power is: 130321

Ejemplo 2:

// Java code to show implementation of
// checkedPow(int b, int k) method of
// Guava's IntMath class
  
import java.math.RoundingMode;
import com.google.common.math.IntMath;
  
class GFG {
  
    static int findPow(int b, int k)
    {
        try {
  
            // Using checkedPow(int b, int k) method of
            // Guava's IntMath class
            // This should raise "ArithmeticException" as
            // b to the kth power overflows in
            // signed int arithmetic
            int ans = IntMath.checkedPow(b, k);
  
            // Return the answer
            return ans;
        }
        catch (Exception e) {
            System.out.println(e);
            return -1;
        }
    }
  
    // Driver code
    public static void main(String args[])
    {
        int b = 20;
        int k = 25;
  
        try {
  
            // Function calling
            findPow(b, k);
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}
Producción:

java.lang.ArithmeticException: overflow

Referencia: https://google.github.io/guava/releases/20.0/api/docs/com/google/common/math/IntMath.html#checkedPow-int-int-

Publicación traducida automáticamente

Artículo escrito por Sahil_Bansall y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *