Java Math log1p() Method
The log1p() method of Math class returns the natural logarithmic sum for the specified double argument and 1. Its value is much closer to result of ln(1 + x).
Syntax:
public static double log1p (double a)
Parameters:
The parameter ‘a’ represents a double value.
Return Value:
The log1p() method returns the natural log of x +1.
Special cases of the log1p() method are as follows:
- It returns NaN, if the argument is NaN or less than -1.
- It returns positive infinity, if the argument passed is positive infinite.
- It returns negative infinity, if the argument passed is negative one.
- It returns zero with the same sign as the argument, if the argument passed is equal to zero.
Example 1:
public class JavaMathLog1pExample1 { public static void main(String[] args) { double a = 10; // returns the natural log of x +1 System.out.println("Logrithm 1p value for "+a +" : "+Math.log1p(a)); } }
Output:
Logrithm 1p value for 10.0 : 2.3978952727983707
Example 2:
public class JavaMathLog1pExample2 { public static void main(String[] args) { double a = -100; //returns NaN, if the argument is NaN or less than -1 System.out.println("Logrithm 1p value for "+a +" : "+Math.log1p(a)); } }
Output:
Logrithm 1p value for -100.0 : NaN
Example 3:
public class JavaMathLog1pExample3 { public static void main(String[] args) { double a = 10/0.0d; // positive infinity, if the argument passed is positive infinite System.out.println("Logrithm 1p value for "+a +" : "+Math.log1p(a)); } }
Output:
Logrithm 1p value for Infinity : Infinity
Example 4:
public class JavaMathLog1pExample4 { public static void main(String[] args) { double a = -1; // returns negative infinity, if the argument passed is negative one System.out.println("Logrithm 1p value for "+a +" : "+Math.log1p(a)); } }
Output:
Logrithm 1p value for -1.0 : -Infinity
Example 5:
public class JavaMathLog1pExample5 { public static void main(String[] args) { double a = -0; //returns zero with the same sign as the argument, if the argument passed is equal to zero System.out.println("Logrithm 1p value for "+a +" : "+Math.log1p(a)); } }
Output:
Logrithm 1p value for 0.0 : 0.0
Example 6:
public class JavaMathLog1pExample6 { public static void main(String[] args) { double a = -100; // returns NaN, if the argument passed is NaN or less than -1 System.out.println("Logrithm 1p value for "+a +" : "+Math.log1p(a)); } }
Output:
Logrithm 1p value for -100.0 : NaN