📌  相关文章
📜  IntMath 课程 |番石榴 |Java

📅  最后修改于: 2022-05-13 01:54:30.090000             🧑  作者: Mango

IntMath 课程 |番石榴 |Java

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

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

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

下表显示了 IntMath Class of Guava 提供的一些方法:

例外:

  • log2 : IllegalArgumentException如果 x <= 0
  • log10 : IllegalArgumentException如果 x <= 0
  • pow : IllegalArgumentException如果 k < 0
  • sqrt : IllegalArgumentException如果 x < 0
  • 除 : ArithmeticException如果 q == 0,或者如果 mode == UNNECESSARY 并且 a 不是 b 的整数倍
  • mod : ArithmeticException如果 m <= 0
  • gcd : IllegalArgumentException如果 a < 0 或 b < 0
  • checkedAdd : ArithmeticException如果 a + b 在有符号整数算术中溢出
  • checkedSubtract : ArithmeticException如果 a – b 在有符号整数算术中溢出
  • checkedMultiply : ArithmeticException如果 a * b 在有符号整数算术中溢出
  • checkedPow : ArithmeticException如果 b 的 k 次幂在有符号 int 算术中溢出
  • 阶乘: IllegalArgumentException如果 n < 0
  • 二项式 : IllegalArgumentException如果 n < 0, kn

番石榴的 IntMath 类提供的其他一些方法是:

示例 1:

// Java code to show implementation of
// IntMath Class of Guava
import 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 Guava
import 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));
    }
}

输出 :

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

参考:谷歌番石榴