Open In App

StrictMath log1p() Method in Java

Improve
Improve
Like Article
Like
Save
Share
Report

The java.lang.StrictMath.log1p() is an in-built method in Java which is used to accept a double value as argument and returns the natural logarithm of the sum of the argument and 1. 
 

Syntax:  

public static double log1p(double x)

Parameters: The function accepts a double value x as parameter and calculates the natural algorithm of (1+x).
Return Values: This method returns the value ln(1+x). The result of log1p(x) is almost as close as the actual result of ln(1 + x) for small values x than the floating-point evaluation of log(1.0+x).
Following cases are considered :  

  • The function returns positive infinity if the argument is positive infinity.
  • The function returns negative infinity for negative infinity.
  • The function returns NaN if the argument is NaN or less than -1.
  • The function returns zero with the sign same as that of the argument if the argument is zero.

Examples:  

Input : 2018.0
Output : 7.610357618312838

Input : -4743.0
Output : NaN

Below programs illustrate the working of java.lang.StrictMath.log1p() function:
Program 1: In this program, finite and non-zero argument is passed. 

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));
    }
}


Output: 

Natural Logarithm of 2019.00567 is 7.610360426629845
Natural Logarithm of 100000.0 is 11.512925464970229

 

Program 2: In this program, infinite and negative arguments are passed.

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));
    }
}


Output: 

Natural Logarithm of Infinity is Infinity
Natural Logarithm of -4742.0 is NaN

 

Reference: https://docs.oracle.com/javase/8/docs/api/java/lang/StrictMath.html#log1p()



Last Updated : 08 Sep, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads