Java - Math log(double) 方法



描述

Java Math log(double a) 返回雙精度值的自然對數(以 e 為底)。特殊情況

  • 如果引數是 NaN 或小於零,則結果為 NaN。

  • 如果引數是正無窮大,則結果是正無窮大。

  • 如果引數是正零或負零,則結果是負無窮大。

宣告

以下是java.lang.Math.log() 方法的宣告

public static double log(double a)

引數

a − 一個值

返回值

此方法返回 ln a 的值,即 a 的自然對數。

異常

獲取正雙精度值的日誌示例

以下示例顯示了 Math log() 方法的用法。

package com.tutorialspoint;

public class MathDemo {

   public static void main(String[] args) {

      // get a double number
      double x = 10.7;

      // print the log of the number
      System.out.println("Math.log(" + x + ")=" + Math.log(x));
   }
}

輸出

讓我們編譯並執行上面的程式,這將產生以下結果:

Math.log(10.7)=2.3702437414678603

獲取零雙精度值的日誌示例

以下示例顯示了 Math log() 方法對零值的用法。

package com.tutorialspoint;

public class MathDemo {

   public static void main(String[] args) {

      // get a double number
      double x = 0.0;

      // print the log of the number
      System.out.println("Math.log(" + x + ")=" + Math.log(x));
   }
}

輸出

讓我們編譯並執行上面的程式,這將產生以下結果:

Math.log(0.0)=-Infinity

獲取負雙精度值的日誌示例

以下示例顯示了 Math log() 方法對負數的用法。

package com.tutorialspoint;

public class MathDemo {

   public static void main(String[] args) {

      // get a double number
      double x = -10.7;

      // print the log of the number
      System.out.println("Math.log(" + x + ")=" + Math.log(x));
   }
}

輸出

讓我們編譯並執行上面的程式,這將產生以下結果:

Math.log(-10.7)=NaN
java_lang_math.htm
廣告