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

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

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

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

句法:

public static int log10(BigInteger x, RoundingMode mode)

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

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

返回值:该方法返回x的以10为底的对数,根据指定的舍入方式进行舍入。

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

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

枚举舍入模式

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.log10() 方法:

示例 1:

// Java code to show implementation of
// log10(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(10000);
  
        // Using log10(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 10 of " + a1
                           + " with rounding mode HALF_EVEN = "
                           + BigIntegerMath
                                 .log10(a1,
                                        RoundingMode.HALF_EVEN));
  
        BigInteger a2 = BigInteger.valueOf(15);
  
        // Using log10(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 10 of " + a2
                           + " with rounding mode HALF_DOWN = "
                           + BigIntegerMath
                                 .log10(a2,
                                        RoundingMode.HALF_DOWN));
    }
}

输出:

Log base 10 of 10000 with rounding mode HALF_EVEN = 4
Log base 10 of 15 with rounding mode HALF_DOWN = 1

示例 2:显示 IllegalArgumentException

// Java code to show implementation of
// log10(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 log10(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 10 of " + a1
                               + " with rounding mode HALF_EVEN = "
                               + BigIntegerMath
                                     .log10(a1,
                                            RoundingMode.HALF_EVEN));
        }
  
        catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}

输出:

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

参考: https: Java