Java Math.log1p() method returns natural logarithm (base e) of the sum of argument and 1. In simple words log1p(num) returns log(num+1).
public class JavaExample { public static void main(String[] args) { double num = 25; //equivalent to log (25+1) i.e log(26) System.out.println(Math.log1p(num)); System.out.println(Math.log(num+1)); } }
Output:
3.258096538021482 3.258096538021482
Syntax of Math.log1p() method
Math.log1p(100); //returns 4.61512051684126
log1p() Description
public static double log1p(double num): It accepts double value as an argument and returns the base e logarithm of (num+1). Here, e is a constant, whose approx. value is 2.71828. The base e logarithm is also known as natural logarithm.
log1p() Parameters
- num: A double value.
log1p() Return Value
- log(num+1).
- If the argument is less than -1 or NaN (Not a Number), then it returns NaN.
- If argument is positive infinity, then it returns positive infinity.
- If the argument is zero, then it returns the zero with the same sign as the argument.
- If the argument is -1, then it returns negative infinity.
Example 1: log1p of zero, positive and negative values
public class JavaExample { public static void main(String[] args) { double num1 = 0, num2 = 10, num3 = -10; System.out.println(Math.log1p(num1)); System.out.println(Math.log1p(num2)); System.out.println(Math.log1p(num3)); } }
Output:
Example 2: log1p of NaN, positive and negative infinity
public class JavaExample { public static void main(String[] args) { double num1 = 0.0/0; //NaN double num2 = 10.0/0; //Positive Infinity double num3 = -10.0/0; //Negative Infinity System.out.println(Math.log1p(num1)); System.out.println(Math.log1p(num2)); System.out.println(Math.log1p(num3)); } }
Output: