Skip to content
Related Articles
Open in App
Not now

Related Articles

Java Math log() method with example

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 23 Mar, 2018
Improve Article
Save Article

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
        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
My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!