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 :

This method returns the value ln a.

Example :To show working of java.lang.Math.log() method.





// 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
        // positive infinity as argument, output Infinity
        // positive zero as argument, output -Infinity
        // positive double as argument




Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :


Please write to us at to report any issue with the above content.