使用 Java 中的 Math.log 獲取自然對數值


要獲取一個數的自然對數,我們使用 java.lang.Math.log() 方法。Math.log() 方法返回一個雙值,其中 a 是雙精度值,即以 e 為底的對數。如果傳遞的值為 NaN 或負值,則結果為 NaN。如果傳遞的值為正無窮大,則返回值為正無窮大。當引數為正零或負零時,則返回負無窮大。

宣告 - java.lang.Math.log() 方法宣告如下

public static double log(double a)

a 是要查詢其自然對數的值。

我們來看一個程式,該程式使用 Math.log() 方法在 Java 中獲取一個數的自然對數

示例

 即時演示

import java.lang.Math;
public class Example {
   public static void main(String[] args) {
      // declaring and initializing two double values
      double x = 300.01d;
      double y = 290.344d;
      // printing their natural logarithms
      System.out.println("Natural logarithm of "+ x + " is " + Math.log(x));
      System.out.println("Natural logarithm of "+ y + " is " + Math.log(y));
   }
}

輸出

Natural logarithm of 300.01 is 5.703815807433991
Natural logarithm of 290.344 is 5.671066426889541

更新於:26-Jun-2020

已檢視 3K+ 次

職業生涯全面起航

透過完成課程獲得認證

開始學習
廣告