.log()
Published Oct 31, 2022Updated Nov 12, 2022
Contribute to Docs
The Math.log()
method returns the natural logarithm of a number.
Syntax
Math.log(a);
- If
a
is a positive number, the return value is adouble
. - If
a
isNaN
or a negative number, the return value isNaN
. - If
a
is positive infinity, the return value is positive infinity. - If
a
is positive or negative zero, the return value is negative infinity.
Example
The following example demonstrates using Math.log()
to find natural logs:
import java.lang.Math;public class Main {public static void main(String args[]) {double a = 10;double b = -6;double c = 0;double d = -0;System.out.println("The result of log(" + a + ") is: " + Math.log(a));System.out.println("The result of log(" + b + ") is: " + Math.log(b));System.out.println("The result of log(" + c + ") is: " + Math.log(c));System.out.println("The result of log(" + d + ") is: " + Math.log(d));}}
This will produce the following output:
The result of log(10.0) is: 2.303The result of log(-6.0) is: NaNThe result of log(0.0) is: -InfinityThe result of log(0.0) is: -Infinity
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.