📜  Java Math log() 方法与示例

📅  最后修改于: 2022-05-13 01:54:39.896000             🧑  作者: Mango

Java Math log() 方法与示例

Java.lang.Math.log()方法返回 double 值的自然对数(以 e 为底)作为参数。有各种情况:

  • 如果参数为NaN 或小于零,则结果为NaN
  • 如果参数是正无穷大,那么结果是正无穷大
  • 如果参数是正零或负零,则结果为负无穷大

句法 :

public static double log(double a)

范围 :

a : User input

返回 :

This method returns the value ln a.

示例:显示Java.lang.Math.log()方法的工作。

// Java program to demonstrate working
// of java.lang.Math.log() method
import java.lang.Math;
  
class Gfg {
      
    // driver code
    public static void main(String args[])
    {
  
        double a = -2.55;
        double b = 1.0 / 0;
        double c = 0, d = 145.256;
          
  
        // negative integer as argument, output NAN
        System.out.println(Math.log(a));
  
        // positive infinity as argument, output Infinity
        System.out.println(Math.log(b));
  
        // positive zero as argument, output -Infinity
        System.out.println(Math.log(c));
          
        // positive double as argument
        System.out.println(Math.log(d));
          
    }
}
输出:
NaN
Infinity
-Infinity
4.978497702968366