Java BigIntegerMath log10() 函数
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: 如果模式是RoundingMode.UNNECESSARY并且x不是10的幂。
Enum RoundingMode
枚举常数 | 描述 |
---|---|
CEILING | 四舍五入模式,朝向正无穷大。 |
DOWN | 向零舍入的舍入模式。 |
FLOOR | 负无穷大的四舍五入模式。 |
HALF_DOWN | 向 “最近的邻居 “舍入的舍入模式,除非两个邻居的距离相等,在这种情况下向下舍入。 |
HALF_EVEN | 向 “最近的邻居 “舍入的舍入模式,除非两个邻居都是等距离,在这种情况下,向偶数邻居舍入。 |
HALF_UP | 向 “最近的邻居 “舍入的舍入模式,除非两个邻居都是等距离,在这种情况下,向上舍入。 |
UNNECESSARY | 四舍五入模式,断言请求的操作有一个精确的结果,因此不需要四舍五入。 |
UP | 舍入模式,从零开始舍入。 |
下面的例子说明了 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://google.github.io/guava/releases/21.0/api/docs/com/google/common/math/BigIntegerMath.html#log10-java.math.BigInteger-java.math.RoundingMode-