# Java log1p() with example

Java 8Object Oriented ProgrammingProgramming

The java.lang.Math.log1p(double x) returns the natural logarithm of the sum of the argument and 1. Note that for small values x, the result of log1p(x) is much closer to the true result of ln(1 + x) than the floating-point evaluation of log(1.0+x).Special cases −

• If the argument is NaN or less than -1, then the result is NaN.

• If the argument is positive infinity, then the result is positive infinity.

• If the argument is negative one, then the result is negative infinity.

• If the argument is zero, then the result is a zero with the same sign as the argument.

## Example

Following is an example to implement the log1p() method in Java −

import java.lang.*;
public class Example {
public static void main(String[] args) {
// get two double numbers
double x = 23878.4;
double y = 1000;
// call log1p and print the result
System.out.println("Math.log1p(" + x + ")=" + Math.log1p(x));
// call log1p and print the result
}
}

## Output

Math.log1p(23878.4)=10.080771441562744
Math.log1p(1000.0)=6.90875477931522

## Example

Let us see another example −

import java.lang.*;
public class Example {
public static void main(String[] args) {
// get two double numbers
double x = -130.25;
double y = 0;
double z = -20;
System.out.println("Math.log1p(" + x + ")=" + Math.log1p(x));
System.out.println("Math.log1p(" + y + ")=" + Math.log1p(y));
System.out.println("Math.log1p(" + y + ")=" + Math.log1p(z));
}
}

## Output

Math.log1p(-130.25)=NaN
Math.log1p(0.0)=0.0
Math.log1p(0.0)=NaN