使用 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+ 次

职业生涯全面起航

通过完成课程获得认证

开始学习
广告
© . All rights reserved.