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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
          
    }
}

chevron_right


Output:

NaN
Infinity
-Infinity
4.978497702968366

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 contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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 :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.