Friday, July 10, 2026
HomeLanguagesJavaJava Math log() method with example

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




// Java program to demonstrate working
// of java.lang.Math.log() method
 
import java.lang.Math;
 
class GFG {
 
    // driver code
    public static void main(String args[])
    {
        double a = -2.55;
        double b = 1.0 / 0;
        double c = 0, d = 145.256;
 
        // negative integer as argument, output NAN
        System.out.println(Math.log(a));
 
        // positive infinity as argument, output Infinity
        System.out.println(Math.log(b));
 
        // positive zero as argument, output -Infinity
        System.out.println(Math.log(c));
 
        // positive double as argument
        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);
    }
}


Output

2.302585092994046
RELATED ARTICLES

2 COMMENTS

Most Popular

Dominic
32519 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6901 POSTS0 COMMENTS
Nicole Veronica
12017 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12111 POSTS0 COMMENTS
Shaida Kate Naidoo
7021 POSTS0 COMMENTS
Ted Musemwa
7263 POSTS0 COMMENTS
Thapelo Manthata
6978 POSTS0 COMMENTS
Umr Jansen
6968 POSTS0 COMMENTS