Java Tutorial Java Advanced Java References

Java Math - log1p() Method



The Java log1p() method returns the natural logarithm of (1 + number), i.e., log(1+number). In special cases it returns the following:

  • 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.

Syntax

public static double log1p(double arg)

Parameters

arg Specify the number.

Return Value

Returns the natural logarithm of (1 + number), i.e., log(1+number).

Exception

NA.

Example:

In the example below, log1p() method is used to calculate the log(1+number).

public class MyClass {
 public static void main(String[] args) {
  System.out.println(Math.log1p(0)); 
  System.out.println(Math.log1p(1)); 
  System.out.println(Math.log1p(2));     
 }
}

The output of the above code will be:

0.0
0.6931471805599453
1.0986122886681096

❮ Java Math Methods

5