當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Math.log()用法及代碼示例

java.lang.Math.log() 用於找出任何數字的對數值。此方法返回雙精度值的自然對數(以 e 為底)作為參數。

用法

public static double log(double x)

參數

x= a value entered by user

返回

This method returns the value In a, the natural logarithm of a.
  • 如果參數為正值,則此方法將返回給定值的對數。
  • 如果參數為負值,則此方法將返回 NaN。
  • 如果參數不是數字 (NaN),則此方法將返回 NaN。
  • 如果參數是 Positive Infinity,則此方法將返回 Positive Infinity。
  • 如果參數是正零或負零,此方法將返回負無窮大。

例子1

public class LogExample1
{
    public static void main(String[] args) 
    {
        double x = 38.9;
        // Input positive double, output logarithm of x  
        System.out.println(Math.log(x));
    }
}

輸出:

3.6609942506244004

例子2

public class LogExample2
{
    public static void main(String[] args) 
    {
        double x = -70.4;
        // Input negative double, output NaN  
        System.out.println(Math.log(x));
    }
}

輸出:

NaN

例子3

public class LogExample3
{
    public static void main(String[] args) 
    {
        double x = 1.0/0;
        // Input positive infinity, output Infinity
        System.out.println(Math.log(x));
    }
}

輸出:

Infinity

示例 4

public class LogExample4
{
    public static void main(String[] args) 
    {
        double x = 0;
        // Input positive zero, output -Infinity
        System.out.println(Math.log(x));
    }
}

輸出:

-Infinity






相關用法


注:本文由純淨天空篩選整理自 Java Math.log() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。