What is the Math log() function in Java?

The static method log() in java.lang.Math class is used to calculate the natural logarithmType of logarithm to the base of the mathematical constant e of an argument. It takes a primitive double type argument x and computes loge(x)log_{e}(x).

Syntax

static double log(double x)

Parameters

x: It takes a number of type double.

Return value

log() will return a natural logarithm of double type.

  • The result is NaN if the argument value is less than zero or NaN.
  • The result is positive infinity (+∞) if the argument value is positive infinity.
  • The result is negative infinity (-∞) if the argument value is a positive zero (+0) or a negative zero (-0).

Code

  • Case #1: A positive value of type double is supplied as an argument. log() will return the natural log.
  • Case #2: A negative value is passed in and log() will return NaN.
  • Case #3: Positive infinity is supplied as an argument and log() will return infinity.
  • Case #4: Negative infinity is supplied as an argument and log() will return NAN.
// Load math library
import java.lang.Math;
// Main class
class EdPresso{
public static void main( String args[] ) {
System.out.println(Math.log(89.3));
}
}
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources