java.lang.Math log(double a)

Description :

This java tutorial shows how to use the log(double a) method of Math class under java.lang package. This method returns the natural logarithm of the method argument.

Method Syntax :

public static double log(double a)

Parameter Input :

DataType Parameter Description
double a the value which will be taking the logarithmic value

Method Returns :

The log(double a) method simply returns the natural logarithm (base e) of a double value.

Special 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.

Compatibility Version :

Requires Java 1.0.2 and up

Exception :

N/A

Java Code Example :

This java example source code demonstrates the use of log(double a) method of Math class. Basically we just get the logarithmic value of the user input.

package com.javatutorialhq.java.examples;

import static java.lang.System.*;

import java.util.Scanner;

/*
 * This example source code demonstrates the use of 
 * log(double a) method of Math class
 * Get the logarithmic value of the user input
 */

public class MathLogarithmDouble {

	public static void main(String[] args) {
		// ask for user input
		out.println("Enter a value:");
		Scanner scan = new Scanner(System.in);
		// use scanner to get user console input
		double value = scan.nextDouble();		
		// get the logarithm value
		double logValue = Math.log(value);
		out.println("logarithm of "+value+" = "+logValue);		
		// close the scanner object to avoid memory leak
		scan.close();		

	}

}

Sample Output :

Running the abs(double a) method example source code of Math class will give you the following output

Enter a value:2.3
logarithm of 2.3 = 0.8329091229351039

Exception Scenario :

N/A

Similar Method :

  • N/A

Suggested Reading List :

References :