Get natural logarithm value using Math.log in Java

Java 8Object Oriented ProgrammingProgramming

To obtain the natural log of a number, we use the java.lang.Math.log() method. The Math.log() method returns the natural logarithm i.e. log to the base e of a double value. If the value passed is NaN or negative, the result is NaN. If the value passed is positive infinity, then the value returned is positive infinity. When the parameter is positive zero or negative zero, then negative infinity is returned.

Declaration - The java.lang.Math.log() method is declared as follows

public static double log(double a)

where a is a value whose natural logarithm is to be found.

Let us see a program to get the natural logarithm of a number in Java using the Math.log() method

Example

 Live Demo

import java.lang.Math;
public class Example {
   public static void main(String[] args) {
      // declaring and initializing two double values
      double x = 300.01d;
      double y = 290.344d;
      // printing their natural logarithms
      System.out.println("Natural logarithm of "+ x + " is " + Math.log(x));
      System.out.println("Natural logarithm of "+ y + " is " + Math.log(y));
   }
}

Output

Natural logarithm of 300.01 is 5.703815807433991
Natural logarithm of 290.344 is 5.671066426889541
raja
Published on 07-Jan-2019 11:29:29
Advertisements