📜  Java中的 StrictMath log() 方法

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

Java中的 StrictMath log() 方法

Java.lang.StrictMath.log()是 StrictMath 类的内置方法,用于计算给定双精度值的自然对数,即以 e 为底的对数。它产生了三个特殊的结果:

  • 当参数为正无穷大时,它返回正无穷大。
  • 当参数为 NaN 或小于零时,它返回 NaN。
  • 当参数为正零或负零时,结果为负无穷大。

句法:

public static double log(double num)

参数:该方法接受一个要求对数的double类型的参数num
返回值:该方法返回num的自然对数值。
例子 :

Input: num = 5.0 
Output: 1.6094379124341003

Input: num = 10.0 
Output:  2.302585092994046

下面的程序说明了Java.lang.StrictMath.log() 方法:
方案一:

java
// Java program to illustrate the
// Java.lang.StrictMath.log() Method
import java.lang.*;
 
public class Geeks {
 
public static void main(String[] args) {
 
    double num1 = 10 , num2 = 25.2 ;
     
    // It returns natural logarithm(base e)
    double log_Value = StrictMath.log(num1);
    System.out.print("Log value of " + num1 + " = " );
    System.out.println(log_Value);
 
    log_Value = StrictMath.log(num2);
    System.out.print("Log value of " + num2 + " = " );
    System.out.println(log_Value);
 
}
}


java
// Java program to illustrate the
// Java.lang.StrictMath.log() Method
import java.lang.*;
 
public class Geeks {
 
public static void main(String[] args) {
 
    double num1 = 0 , num2 = (1.0/0.0) , num3 = 1;
     
    // It returns natural logarithm(base e)
    double log_Value = StrictMath.log(num1);
    System.out.print("Log value of " + num1 + " = " );
    System.out.println(log_Value);
     
    log_Value = StrictMath.log(num2);
    System.out.print("Log value of " + num2 + " = " );
    System.out.println(log_Value);
     
    log_Value = StrictMath.log(num3);
    System.out.print("Log value of " + num3 + " = " );
    System.out.println(log_Value);
 
}
}


输出:
Log value of 10.0 = 2.302585092994046
Log value of 25.2 = 3.2268439945173775

方案二:

Java

// Java program to illustrate the
// Java.lang.StrictMath.log() Method
import java.lang.*;
 
public class Geeks {
 
public static void main(String[] args) {
 
    double num1 = 0 , num2 = (1.0/0.0) , num3 = 1;
     
    // It returns natural logarithm(base e)
    double log_Value = StrictMath.log(num1);
    System.out.print("Log value of " + num1 + " = " );
    System.out.println(log_Value);
     
    log_Value = StrictMath.log(num2);
    System.out.print("Log value of " + num2 + " = " );
    System.out.println(log_Value);
     
    log_Value = StrictMath.log(num3);
    System.out.print("Log value of " + num3 + " = " );
    System.out.println(log_Value);
 
}
}
输出:
Log value of 0.0 = -Infinity
Log value of Infinity = Infinity
Log value of 1.0 = 0.0