Java Math log1p() Method

java math log1p() method

Java Math Class

Java Math class provides several methods to perform several operations on math calculations like max(), min(), sin(), cos(), round(), ceil(), floor(), abs() etc.
The java.lang.Math class contains various methods for performing basic numeric operations

Here, in the page we will discuss about the math log1p() Method in java.

Java Math log1p() Method :

In Java, the Math.log1p(double x) method returns the natural logarithm (base e) of the sum of the argument and 1. This method is useful for calculating the logarithm of a number that is very close to 1, as the log1p() method is more accurate than the Math.log() method when the argument is close to 0.

The log1p() method takes a single parameter, x, which is the value for which the logarithm is to be calculated. It returns a double value representing the natural logarithm of x + 1.

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.

Syntax  :

public static double log1p(double x)

Example 1 :  

Run
public class Main {
    public static void main(String[] args) {
        double x = 0.1;
        double result = Math.log1p(x);
        System.out.println(result); // prints 0.09531017980432487
    }
}

Output :

0.09531017980432487

Example 2:  

Run
public class Main {
    public static void main(String[] args) {
        double x1 = Double.NaN;
        double x2 = -1.1;
        double x3 = Double.POSITIVE_INFINITY;
        double x4 = -1.0;
        double x5 = 0.0;
        
        double result1 = Math.log1p(x1);
        double result2 = Math.log1p(x2);
        double result3 = Math.log1p(x3);
        double result4 = Math.log1p(x4);
        double result5 = Math.log1p(x5);
        
        System.out.println(result1); // prints NaN
        System.out.println(result2); // prints NaN
        System.out.println(result3); // prints Infinity
        System.out.println(result4); // prints -Infinity
        System.out.println(result5); // prints 0.0
    }
}

Output :

NaN
NaN
Infinity
-Infinity
0.0

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription