Java Math log() method with example
The java.lang.Math.log() method returns the natural logarithm (base e) of a double value as a parameter. There are various cases :
- If the argument is NaN or less than zero, then the result is NaN.
- If the argument is positive infinity, then the result is positive infinity.
- If the argument is positive zero or negative zero, then the result is negative infinity.
Syntax :
public static double log(double a)
Parameter :
a : User input
Return Type:
This method returns the value ln a.
Example 1: To show the working of java.lang.Math.log() method.
java
import java.lang.Math;
class GFG {
public static void main(String args[])
{
double a = - 2.55 ;
double b = 1.0 / 0 ;
double c = 0 , d = 145.256 ;
System.out.println(Math.log(a));
System.out.println(Math.log(b));
System.out.println(Math.log(c));
System.out.println(Math.log(d));
}
}
|
Output:
NaN
Infinity
-Infinity
4.978497702968366
Example 2:
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
double number = 10.0 ;
double result = Math.log(number);
System.out.println(result);
}
}
|
Last Updated :
04 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...