Java Math log10()方法計算指定值的以10為底的對數(shù),然后將其返回。
log10()方法的語法為:
Math.log10(double x)
注意:該log10()方法是靜態(tài)方法。因此,我們可以使用類名直接調(diào)用該方法Math。
x - 要計算其對數(shù)的值
返回x的以10為底的對數(shù)
如果x為NaN或小于零,則返回NaN
如果x為正無窮大,則返回正無窮大
如果x為零,則返回負無窮大
注意:當n是整數(shù)時,值為 log10(10n) = n
class Main { public static void main(String[] args) { //計算雙精度值的log10() System.out.println(Math.log10(9.0)); // 0.9542425094393249 //計算0的log10() System.out.println(Math.log10(0.0)); // -Infinity //計算NaN的log10() double nanValue = Math.sqrt(-5.0); System.out.println(Math.log10(nanValue)); // NaN //計算無窮大的log10() double infinity = Double.POSITIVE_INFINITY; System.out.println(Math.log10(infinity)); // Infinity //計算負數(shù)的log10() System.out.println(Math.log(-9.0)); // NaN //計算10的3次方的log10() System.out.println(Math.log10(Math.pow(10, 3))); // 3.0 } }
在上面的示例中,請注意以下表達式:
Math.log10(Math.pow(10, 3))
在這里,Math.pow(10, 3)返回103。要了解更多信息,請訪問Java Math.pow()。