Java Math log()方法計算指定值的自然對數(shù)(以e為底)并返回它。
log()方法的語法為:
Math.log(double x)
注意:log()方法是靜態(tài)方法。因此,我們可以使用類名Math直接調(diào)用該方法。
x - 要計算其對數(shù)的值
返回x的自然對數(shù)(即ln a)
如果參數(shù)為NaN或小于零,則返回NaN
如果參數(shù)為正無窮大,則返回正無窮大
如果參數(shù)為零,則返回負無窮大
class Main { public static void main(String[] args) { // 計算雙精度值的log() System.out.println(Math.log(9.0)); // 2.1972245773362196 //計算零的log() System.out.println(Math.log(0.0)); // -Infinity //計算NaN的log() double nanValue = Math.sqrt(-5.0); System.out.println(Math.log(nanValue)); // NaN //計算無窮大的log() double infinity = Double.POSITIVE_INFINITY; System.out.println(Math.log(infinity)); // Infinity //計算負數(shù)的log() System.out.println(Math.log(-9.0)); // NaN } }