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


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

在本教程中,我們將借助示例了解 Java Math log() 方法。

log() 方法計算指定值的自然對數(以 e 為底)並將其返回。

示例

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

    // compute log() of 9
    System.out.println(Math.log(9.0));


  }
}

// Output: 2.1972245773362196

數學語法。log()

用法:

Math.log(double x)

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

參數:

  • x- 要計算其對數的值

log() 返回值

  • 返回 x 的自然對數(即 ln a )
  • 如果參數為 NaN 或小於零,則返回 NaN
  • 如果參數是正無窮大,則返回正無窮大
  • 如果參數為零,則返回負無窮大

示例:Java 數學。log()

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

    // compute log() for double value
    System.out.println(Math.log(9.0));       // 2.1972245773362196

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


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


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


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


  }
}

相關用法


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