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 } }