Java BigIntegerMath类

Java BigIntegerMath类

BigIntegerMath 用于对BigInteger值进行数学运算。基本的独立数学函数根据所涉及的主要数字类型被分为 IntMath、LongMath、DoubleMath和BigIntegerMath 类。这些类具有平行结构,但每个类只支持相关的函数子集。对于int和long的类似功能可以分别在IntMath和LongMath中找到。

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

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

下表显示了Guava的BigIntegerMath类所提供的方法。

BigIntegerMath类  Guava  Java

异常情况:

  • log2 : 如果x<=0,则出现非法参数异常
  • log10 : 如果x<=0,会出现非法的参数异常。
  • sqrt : 如果x<0,会出现IllegalArgumentException。
  • divide : 如果q == 0,或者mode == UNNECESSARY,并且a不是b的整数倍,则出现ArithmeticException。
  • factorial: 如果n<0,则出现IllegalArgumentException。
  • binomial: 如果n<0,k n,则出现非法参数异常。

例1 :

// Java code to show implementation of
// BigIntegerMath Class of Guava
import java.math.*;
import com.google.common.math.BigIntegerMath;
  
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 10 is
            // not completely divisible by 3
            // thus rounding is required, and
            // RoundingMode is set as UNNESSARY
            System.out.println(BigIntegerMath.divide(BigInteger.TEN,
                                                new BigInteger("3"), 
                                           RoundingMode.UNNECESSARY));
        }
        catch (ArithmeticException ex) {
            System.out.println("Error Message is : " +
                                       ex.getMessage());
        }
    }
}

输出。

Error Message is : Rounding necessary

例2 :

// Java code to show implementation of
// BigIntegerMath Class of Guava
import java.math.*;
import com.google.common.math.BigIntegerMath;
  
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 10 is divisible by 5, so
        // no exception is thrown
        System.out.println(BigIntegerMath.divide(BigInteger.TEN,
                                        new BigInteger("5"),
                                    RoundingMode.UNNECESSARY));
  
        // To compute log to base 10
        System.out.println("Log10 is : " + 
             BigIntegerMath.log10(new BigInteger("1000"), 
                                 RoundingMode.HALF_EVEN));
  
        // To compute factorial
        System.out.println("factorial is : " + 
                         BigIntegerMath.factorial(7));
  
        // To compute log to base 2
        System.out.println("Log2 is : " + 
           BigIntegerMath.log2(new BigInteger("8"),
                          RoundingMode.HALF_EVEN));
  
        // To compute square root
        System.out.println("sqrt is : " +       
                    BigIntegerMath.sqrt(BigInteger.
                    TEN.multiply(BigInteger.TEN),
                        RoundingMode.HALF_EVEN));
    }
}

输出。

2
Log10 is : 3
factorial is : 5040
Log2 is : 3
sqrt is : 10

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程