當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Math log10()用法及代碼示例

Java Math log10() 方法計算指定值的以 10 為底的對數並將其返回。

用法:

Math.log10(double x)

在這裏,log10() 是一個靜態方法。因此,我們直接使用類名 Math 調用該方法。

參數:

  • x- 要計算其對數的值

log10() 返回值

  • 返回以 10 為底的對數x
  • 如果返回 NaNx為 NaN 或小於零
  • 如果返回正無窮大x是正無窮大
  • 如果返回負無窮大x為零

注意: 的價值log10(10n) = n,其中n是一個整數。

示例:Java Math.log10()

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

    // compute log10() for double value
    System.out.println(Math.log10(9.0));       // 0.9542425094393249

    // compute log10() for zero
    System.out.println(Math.log10(0.0));       // -Infinity

    // compute log10() for NaN
    double nanValue = Math.sqrt(-5.0);
    System.out.println(Math.log10(nanValue));  // NaN

    // compute log10() for infinity
    double infinity = Double.POSITIVE_INFINITY;
    System.out.println(Math.log10(infinity));  // Infinity

    // compute log10() for negative numbers
    System.out.println(Math.log(-9.0));      // NaN

    //compute log10() for 103
    System.out.println(Math.log10(Math.pow(10, 3)));  // 3.0

  }
}

在上麵的例子中,注意表達式,

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

在這裏, Math.pow(10, 3) 返回 103 。要了解更多信息,請訪問 Java Math.pow()

相關用法


注:本文由純淨天空篩選整理自 Java Math log10()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。