Java Math log()

log() accepts a double value as an argument, and returns the natural logarithm of the argument. The return value is of type double.

Following is the syntax of log() method.

double value = log(double x)

Since the definition of log() function has double datatype as argument, you can pass int, float or long as arguments; because these datatypes could implicitly promote to double.

We shall learn about some of the special cases for log() method with examples.

Example 1 – Math.log(double)

In the following example, we use log() method to find the square root of 10.

Java Program

public class MathExample {
	public static void main(String[] args) {
		double x = 10;
		double log_x = Math.log(x);
		System.out.println(log_x);
	}
}

Output

2.302585092994046
ADVERTISEMENT

Example 2 – Math.log(int)

In the following example, we pass an int value for the argument to log() method.

Java Program

public class MathExample {
	public static void main(String[] args) {
		int x = 4;
		double log_x = Math.log(x);
		System.out.println(log_x);
	}
}

Output

1.3862943611198906

Similarly, you can provide a float or long value as argument to log() method.

Example 3 – Math.log(NaN)

In the following example, we pass Double.NaN as argument to log() method. As per the definition of the log() in Math class, the method should return NaN value.

Java Program

public class MathExample {
	public static void main(String[] args) {
		double x = Double.NaN;
		double log_x = Math.log(x);
		System.out.println(log_x);
	}
}

Output

NaN

Example 4 – Math.log() – With Positive Infinity as Argument

In the following example, we pass Double.POSITIVE_INFINITY as argument to log() method. As per the definition of the log() method in Math class, the method should return positive infinity.

Java Program

public class MathExample {
	public static void main(String[] args) {
		double x = Double.POSITIVE_INFINITY;
		double log_x = Math.log(x);
		System.out.println(log_x);
	}
}

Output

Infinity

Example 5 – Math.log() – With Negative Infinity as Argument

In the following example, we pass Double.POSITIVE_INFINITY as argument to log() method. As per the definition of the log() method in Math class, the method should return NaN for negative infinity as input value.

Java Program

public class MathExample {
	public static void main(String[] args) {
		double x = Double.NEGATIVE_INFINITY;
		double log_x = Math.log(x);
		System.out.println(log_x);
	}
}

Output

NaN

Example 6 – Math.log() – With zero as Argument

In the following example, we pass zero as argument to log() method. As per the definition of the log() method in Math class, the method should return negative Infinity for zero as input value.

Java Program

public class MathExample {
	public static void main(String[] args) {
		double x = 0;
		double log_x = Math.log(x);
		System.out.println(log_x);
	}
}

Output

-Infinity

Conclusion

In this Java Tutorial, we learned about Java Math.log() function, with example programs.