📜  Java中的 StrictMath log1p() 方法

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

Java中的 StrictMath log1p() 方法

Java.lang.StrictMath.log1p()是Java中的一个内置方法,用于接受双精度值作为参数,并返回参数和 1 的自然对数

句法:

public static double log1p(double x)

参数:该函数接受一个 double 值 x 作为参数,并计算 (1+x) 的自然算法
返回值:此方法返回值ln(1+x) 。对于小值xlog1p(x)的结果几乎与ln(1 + x)的实际结果一样接近log(1.0+x)的浮点计算。
考虑以下情况:

  • 如果参数是正无穷大,则函数返回正无穷大。
  • 该函数为负无穷大返回负无穷大。
  • 如果参数为NaN小于 -1 ,则函数返回NaN
  • 如果参数为零,则函数返回,其符号与参数的符号相同。

例子:

Input : 2018.0
Output : 7.610357618312838

Input : -4743.0
Output : NaN

下面的程序说明了Java.lang.StrictMath.log1p()函数的工作:
程序 1:在此程序中,传递了有限且非零的参数。

Java
// Java Program to illustrate
// StrictMath.log1p() function
 
import java.io.*;
import java.lang.*;
 
class GFG {
    public static void main(String[] args)
    {
 
        double val1 = 2018.00567, val2 = 99999.0;
 
        System.out.println("Natural Logarithm of " +
        (val1 + 1) + " is " + StrictMath.log1p(val1));
 
        System.out.println("Natural Logarithm of " +
        (val2 + 1) + " is " + StrictMath.log1p(val2));
    }
}


Java
// Java Program to illustrate
// StrictMath.log1p() function
 
import java.io.*;
import java.lang.*;
 
class GFG {
    public static void main(String[] args)
    {
 
        double val1 = 201800 / (0.0), val2 = -4743.0;
 
        System.out.println("Natural Logarithm of " +
        (val1 + 1) + " is " + StrictMath.log1p(val1));
 
        System.out.println("Natural Logarithm of " +
        (val2 + 1) + " is " + StrictMath.log1p(val2));
    }
}


输出:
Natural Logarithm of 2019.00567 is 7.610360426629845
Natural Logarithm of 100000.0 is 11.512925464970229

程序 2:在这个程序中,传递了无限和否定的参数。

Java

// Java Program to illustrate
// StrictMath.log1p() function
 
import java.io.*;
import java.lang.*;
 
class GFG {
    public static void main(String[] args)
    {
 
        double val1 = 201800 / (0.0), val2 = -4743.0;
 
        System.out.println("Natural Logarithm of " +
        (val1 + 1) + " is " + StrictMath.log1p(val1));
 
        System.out.println("Natural Logarithm of " +
        (val2 + 1) + " is " + StrictMath.log1p(val2));
    }
}
输出:
Natural Logarithm of Infinity is Infinity
Natural Logarithm of -4742.0 is NaN

参考: https: Java/lang/StrictMath.html#log1p()