Join our newsletter for the latest updates.

Java Math log1p()

The Java Math log1p() method returns the natural logarithm (base e) of the sum of specified value and 1.

The syntax of the log1p() method is:

Math.log1p(double x)

Here, log1p() is a static method. Hence, we are calling the method directly using the class name Math.

log1p() Parameters

The log1p() method takes a single parameter.

  • x - the value whose logarithm is to be computed

log1p() Return Values

  • returns the natural logarithm of x + 1
  • returns NaN if x is NaN or less than -1
  • returns positive infinity if x is positive infinity
  • returns zero if x is zero

Example1 : Java Math.log1p()

class Main {
  public static void main(String[] args) {

    // log1p() for double value
    System.out.println(Math.log1p(9.0));       // 2.302585092994046

    // log1p() for zero
    System.out.println(Math.log1p(0.0));       // 0.0

    // log1p() for NaN
    // square root of negative number is NaN
    double nanValue = Math.sqrt(-5.0);
    System.out.println(Math.log1p(nanValue));  // NaN

    // log1p() for infinity
    double infinity = Double.POSITIVE_INFINITY;
    System.out.println(Math.log1p(infinity));  // Infinity

    // log1p() for negative numbers
    System.out.println(Math.log(-9.0));        // NaN


In the above example, notice the expression,

Math.log1p(Math.pow(10, 3))

Here, Math.pow(10, 3) returns 103. To learn more, visit Java Math.pow().

Note: We have used the Math.sqrt() method to calculate the square root of -5. The square root of negative numeber is not a number.

Example 2: Math.log1p() and Math.log()

class Main {
  public static void main(String[] args) {

    double a = 9.0;
    // log1p() for double value
    System.out.println(Math.log1p(a));   // 2.302585092994046

    // Compute log() for a + 1
    a = a + 1;
    System.out.println(Math.log(a));    // 2.302585092994046

    // Here you can see log1p(x) == log(x + 1)


Recommended Tutorials: