log()
方法计算指定值的自然对数(以 e 为底)并返回它。
示例
class Main {
public static void main(String[] args) {
// compute log() of 9
System.out.println(Math.log(9.0));
}
}
// Output: 2.1972245773362196
Math.log() 语法
log()
方法的语法是
Math.log(double x)
这里,log()
是一个静态方法。因此,我们直接通过类名 Math
调用该方法。
log() 参数
- x - 要计算对数的数值
log() 返回值
- 返回 x 的自然对数(即
ln a
) - 如果参数是 NaN 或小于零,则返回 NaN
- 如果参数是正无穷大,则返回正无穷大
- 如果参数为零,则返回负无穷大
示例:Java Math.log()
class Main {
public static void main(String[] args) {
// compute log() for double value
System.out.println(Math.log(9.0)); // 2.1972245773362196
// compute log() for zero
System.out.println(Math.log(0.0)); // -Infinity
// compute log() for NaN
double nanValue = Math.sqrt(-5.0);
System.out.println(Math.log(nanValue)); // NaN
// compute log() for infinity
double infinity = Double.POSITIVE_INFINITY;
System.out.println(Math.log(infinity)); // Infinity
// compute log() for negative numbers
System.out.println(Math.log(-9.0)); // NaN
}
}
另请阅读