Guava IntMath类

来源:这里教程网 时间:2026-02-17 21:41:35 作者:

Guava IntMath类

简介 : IntMath 是用来对整数值进行数学运算的。基本的独立数学函数根据所涉及的主要数字类型被分为IntMath、LongMath、DoubleMath和BigIntegerMath类。这些类具有平行结构,但每个类只支持相关的函数子集。

声明 : com.google.common.math.IntMath类的声明是。

@GwtCompatible(emulated = true)public final class IntMath   extends Object

下表显示了Guava的IntMath类所提供的一些方法。

异常情况:

log2 : 如果x<=0,则出现IllegalArgumentExceptionlog10 : 如果x<=0,则出现IllegalArgumentExceptionpow : 如果k<0,则出现IllegalArgumentException。sqrt : 如果x<0,则出现IllegalArgumentExceptiondivide : 如果q == 0,或者mode == UNNECESSARY,并且a不是b的整数倍,则出现ArithmeticException。mod : 如果m<=0,则出现算术异常gcd : 如果a<0或b<0,则出现IllegalArgumentException。checkedAdd : 如果a+b在有符号int算术中溢出,则出现ArithmeticExceptioncheckedSubtract : 如果a-b在有符号整数运算中溢出,则会出现ArithmeticExceptioncheckedMultiply : 如果a * b在有符号int算术中溢出,则出现ArithmeticExceptioncheckedPow : 如果b在有符号的int算术中溢出到第k次幂,则出现ArithmeticExceptionfactorial : 如果n<0,则出现IllegalArgumentExceptionbinomial : 如果n<0,k n,则出现IllegalArgumentException。

Guava的IntMath类提供的其他一些方法有:

例1 :

// Java code to show implementation of// IntMath Class of Guavaimport java.math.RoundingMode;import com.google.common.math.IntMath;  class GFG {      // Driver code    public static void main(String args[])    {          // Creating an object of GFG class        GFG obj = new GFG();          // Function calling        obj.examples();    }      private void examples()    {          try {              // exception will be thrown as 80 is not            // completely divisible by 3            // thus rounding is required, and            // RoundingMode is set as UNNESSARY            System.out.println(IntMath.divide(80, 3,                           RoundingMode.UNNECESSARY));        }        catch (ArithmeticException ex) {            System.out.println("Error Message is : " +                                      ex.getMessage());        }    }}

输出:

Error Message is : mode was UNNECESSARY, but rounding was necessary

例2 :

// Java code to show implementation of// IntMath Class of Guavaimport java.math.RoundingMode;import com.google.common.math.IntMath;  class GFG {      // Driver code    public static void main(String args[])    {        // Creating an object of GFG class        GFG obj = new GFG();          // Function calling        obj.examples();    }      private void examples()    {          // As 120 is divisible by 4, so        // no exception is thrown        System.out.println(IntMath.divide(120, 4,                        RoundingMode.UNNECESSARY));          // To compute GCD of two integers        System.out.println("GCD is : " +                              IntMath.gcd(70, 14));          // To compute log to base 10        System.out.println("Log10 is : " +          IntMath.log10(1000, RoundingMode.HALF_EVEN));          // To compute remainder        System.out.println("modulus is : " +                          IntMath.mod(125, 5));          // To compute factorial        System.out.println("factorial is : " +                           IntMath.factorial(7));          // To compute log to base 2        System.out.println("Log2 is : " +                IntMath.log2(8, RoundingMode.HALF_EVEN));          // To compute square root        System.out.println("sqrt is : " +                    IntMath.sqrt(IntMath.pow(12, 2),                          RoundingMode.HALF_EVEN));    }}

输出:

30GCD is : 14Log10 is : 3modulus is : 0factorial is : 5040Log2 is : 3sqrt is : 12

参考资料 : Google Guava

相关推荐