log() Method in Java
Table of Content:
Description
This Java tutorial shows how to use the log(double a) method of Math class under java.lang package.
The method returns the natural logarithm of the argument.
Syntax
double log(double d)
Parameters
Here is the detail of parameters ?
-
d ? Any primitive data type.
Return Value
- This method returns the natural logarithm of the argument.
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.
Example
public class MathLogMethod { public static void main(String args[]) { double x = 11.635; double y = 2.76; System.out.printf("The value of e is %.4f%n", Math.E); System.out.printf("log(%.3f) is %.3f%n", x, Math.log(x)); } }
Output
The value of e is 2.7183 log(11.635) is 2.454 Press any key to continue . . .
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.
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(); } }
Output
Running the log(double a) method example source code of Math class will give you the following output
Enter a value: 10 logarithm of 10.0 = 2.302585092994046 Press any key to continue . . .
Find natural logarithm value of a number using Math.log
/* Find natural logarithm value of a number using Math.log This java example shows how to find natural logarithm value of a number using log method of Java Math class. */ public class FindNaturalLogarithmOfNumberExample { public static void main(String[] args) { /* * To find natural logarithm value of a number, use * static double log(double d) method of Java Math class. */ System.out.println("Natural logarithm value of 2 is : " + Math.log(2)); } }
Output
Running the log(double a) method example source code of Math class will give you the following output
Natural logarithm value of 2 is : 0.6931471805599453 Press any key to continue . . .