Java log1p()带示例

原文:https://www.geeksforgeeks.org/java-log1p-example/

java.lang.Math.log1p() 是 Java Math Library 方法之一,用于返回参数和 1 之和的自然对数。对于小值,log1p(a)的结果比 log(1.0 + a)的浮点计算更接近 ln(1 + a)的真实结果。有各种情况:

  • 如果参数为正双精度值,Math.log1p()方法将返回给定值的对数。
  • 如果参数为 NaN 或小于-1 ,Math.log1p()方法将返回 NaN
  • 如果参数为正无穷大,Math.log1p()方法将返回结果为正无穷大
  • 如果参数为负一,Math.log1p()方法将返回负无穷大
  • 如果参数是正或负零,Math.log1p()方法将返回

语法:

public static double log1p(double a)

参数:

a : User input

返回:

This method returns the value ln(x + 1), the natural log of x + 1.

示例:展示 java.lang.Math.log1p() 方法的工作。

// Java program to demonstrate working
// of java.lang.Math.log1p() method
import java.lang.Math;

class Gfg {

    // driver code
    public static void main(String args[])
    {
        double a = 23.45;
        double b = -145.25;
        double c = 1.0 / 0;
        double d = -1;
        double e = 0;

         // positive double value as argument,
        // output double value
        System.out.println(Math.log1p(a));

         // negative integer as argument,
        // output NAN
        System.out.println(Math.log1p(b));

         // positive infinity as argument,
        // output Positive Infinity
        System.out.println(Math.log1p(c));

        // negative one as argument,
        // output Negative Infinity
        System.out.println(Math.log1p(d));

        // positive zero as argument,
        // output Zero
        System.out.println(Math.log1p(e));
    }
}

Output:

3.196630215920881
NaN
Infinity
-Infinity
0.0