跳到主要内容

Java Math log10() 方法

log10() 方法的语法是:

Math.log10(double x)

这里,log10() 是一个静态方法。因此,我们直接使用类名 Math 来调用这个方法。

log10() 参数

  • x - 需要计算对数的值

log10() 返回值

  • 如果 x 是正无穷大,则返回正无穷大
  • 如果 x 是 0,则返回负无穷大
  • 返回 x 的以 10 为底的对数
  • 如果 x 是 NaN 或小于零,则返回 NaN

注意log10(10n) = n,其中 n 是一个整数。

示例:Java Math.log10()

class Main {
public static void main(String[] args) {

// 计算双精度值的 log10()
System.out.println(Math.log10(9.0)); // 0.9542425094393249

// 计算零的 log10()
System.out.println(Math.log10(0.0)); // -Infinity

// 计算 NaN 的 log10()
double nanValue = Math.sqrt(-5.0);
System.out.println(Math.log10(nanValue)); // NaN

// 计算无穷大的 log10()
double infinity = Double.POSITIVE_INFINITY;
System.out.println(Math.log10(infinity)); // Infinity

// 计算负数的 log10()
System.out.println(Math.log(-9.0)); // NaN

// 计算 10^3 的 log10()
System.out.println(Math.log10(Math.pow(10, 3))); // 3.0

}
}

在上面的示例中,请注意这个表达式,

Math.log10(Math.pow(10, 3))

这里,Math.pow(10, 3) 返回 1000。要了解更多,请访问 Java Math.pow()

推荐教程