Java Tutorial Java Advanced Java References

Java Math - log() Method



The Java log() method returns the natural logarithm (base e) of a given number. In special cases it returns the following:

  • 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 arg)

Parameters

arg Specify the number.

Return Value

Returns the natural logarithm of a given number.

Exception

NA.

Example:

In the example below, log() method is used to calculate the natural logarithm of a given number.

public class MyClass {
 public static void main(String[] args) {
  System.out.println(Math.log(10)); 
  System.out.println(Math.log(50)); 
  System.out.println(Math.log(100));     
 }
}

The output of the above code will be:

2.302585092994046
3.912023005428146
4.605170185988092

❮ Java Math Methods