Java Math log() Method
The log() method of Math class returns the natural logarithmic value for the specified double argument.
Syntax:
public static double log(double a)
Parameters:
The parameter ‘a’ represents the value.
Return Value:
The log() method returns the natural base e logarithm of a( ln a).
Special cases of the log() method are as follows:
- It returns NaN , if the argument is NaN or less than zero.
- It returns positive infinity, if the argument passed is positive infinite.
- It returns negative infinity, if the argument passed is positive zero or negative zero.
Example 1:
public class JavaMathLogExample1 { public static void main(String[] args) { double a = 10; //returns the natural base e logarithm of a System.out.println("Log value : "+Math.log(a)); } }
Output:
Log value : 2.302585092994046
Example 2:
public class JavaMathLogExample2 { public static void main(String[] args) { double a = -4/0.0d; //returns NaN, if argument is NaN or less than 0 System.out.println("Log value : "+Math.log(a)); } }
Output:
Log value : NaN
Example 3:
public class JavaMathLogExample3 { public static void main(String[] args) { double a = 4/0.0d; //returns positive infinity, if argument is infinite System.out.println("Log value : "+Math.log(a)); } }
Output:
Log value : Infinity
Example 4:
public class JavaMathLogExample4 { public static void main(String[] args) { double a = 0; //returns negative infinity, if the argument is positive or negative 0 System.out.println("Log value : "+Math.log(a)); } }
Output:
Log value : -Infinity