📜  BigIntegerMath log2()函数|番石榴 |Java

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

BigIntegerMath log2()函数|番石榴 |Java

Guava 的 BigIntegerMath 类的方法log2(BigInteger x, RoundingMode mode)返回 x 的以 2 为底的对数,根据指定的舍入模式进行舍入。

句法:

public static int log2(BigInteger x, RoundingMode mode)

参数:此方法采用以下参数:

  • x :要查找其日志的 BigInteger 数。
  • mode : 计算 2 对数的舍入模式。

返回值:该方法返回x的以2为底的对数,按照指定的舍入方式进行舍入。

异常:此方法引发以下异常:

  • IllegalArgumentException:如果 x <= 0。
  • ArithmeticException:如果 mode 是 RoundingMode.UNNECESSARY 并且 x 不是 2 的幂。

枚举舍入模式

Enum ConstantDescription
CEILINGRounding mode to round towards positive infinity.
DOWNRounding mode to round towards zero.
FLOORRounding mode to round towards negative infinity.
HALF_DOWNRounding mode to round towards “nearest neighbor” unless both neighbors are equidistant, in which case round down.
HALF_EVENRounding mode to round towards the “nearest neighbor” unless both neighbors are equidistant, in which case, round towards the even neighbor.
HALF_UPRounding mode to round towards “nearest neighbor” unless both neighbors are equidistant, in which case round up.
UNNECESSARYRounding mode to assert that the requested operation has an exact result, hence no rounding is necessary.
UPRounding mode to round away from zero.

下面的示例说明了 BigIntegerMath.log2() 方法:

示例 1:

// Java code to show implementation of
// log2(BigInteger x, RoundingMode mode) method
// of Guava's BigIntegerMath class
  
import java.math.*;
import com.google.common.math.BigIntegerMath;
  
class GFG {
  
    // Driver code
    public static void main(String args[])
    {
        BigInteger a1 = BigInteger.valueOf(1024);
  
        // Using log2(BigInteger x, RoundingMode mode)
        // method of Guava's BigIntegerMath class
        // The RoundingMode HALF_EVEN rounds towards
        // the "nearest neighbor" unless both neighbors
        // are equidistant, in which case, round towards
        // the even neighbor.
        System.out.println("Log base 2 of " + a1
                           + " with rounding mode HALF_EVEN = "
                           + BigIntegerMath
                                 .log2(a1,
                                        RoundingMode.HALF_EVEN));
  
        BigInteger a2 = BigInteger.valueOf(15);
  
        // Using log2(BigInteger x, RoundingMode mode)
        // method of Guava's BigIntegerMath class
        // The RoundingMode HALF_DOWN rounds towards
        // "nearest neighbor" unless both neighbors
        // are equidistant, in which case round down.
        System.out.println("Log base 2 of " + a2
                           + " with rounding mode HALF_DOWN = "
                           + BigIntegerMath
                                 .log10(a2,
                                        RoundingMode.HALF_DOWN));
    }
}

输出:

Log base 2 of 1024 with rounding mode HALF_EVEN = 10
Log base 2 of 15 with rounding mode HALF_DOWN = 1

示例 2:显示 IllegalArgumentException

// Java code to show implementation of
// log2(BigInteger x, RoundingMode mode) method
// of Guava's BigIntegerMath class
  
import java.math.*;
import com.google.common.math.BigIntegerMath;
  
class GFG {
  
    // Driver code
    public static void main(String args[])
    {
  
        try {
            BigInteger a1 = BigInteger.valueOf(-5);
  
            // Using log2(BigInteger x, RoundingMode mode)
            // method of Guava's BigIntegerMath class
            // The RoundingMode HALF_EVEN rounds towards
            // the "nearest neighbor" unless both neighbors
            // are equidistant, in which case, round towards
            // the even neighbor.
            // This should throw "IllegalArgumentException"
            // as a1 <= 0
            System.out.println("Log base 2 of " + a1
                               + " with rounding mode HALF_EVEN = "
                               + BigIntegerMath
                                     .log2(a1,
                                            RoundingMode.HALF_EVEN));
        }
  
        catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}

输出:

Exception: java.lang.IllegalArgumentException: x (-5) must be > 0

参考: https: Java