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


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

JavaScript中的Math.log()函數用於返回數字的自然對數(以e為底)。 JavaScript Math.log()函數在數學上等效於ln(x)。
如果x的值為負,則math.log()函數將返回NaN。
log()是Math的靜態方法,因此,始終用作Math.log(),而不是用作創建的Math對象的方法。

用法:

Math.log(value)

參數:此函數接受單個參數值,該值是您要查找其自然對數的數字。


返回值:Math.log()函數返回給定數字的自然對數。

例子:

  • 將零作為參數傳遞時。
    <script type="text/javascript"> 
       document.write("Output : " + Math.log(0)); 
    </script>

    輸出:

    Output : -Infinity
  • 當“-1”作為參數傳遞時。
    <script type="text/javascript"> 
       document.write("Output : " + Math.log(-1)); 
    </script>

    輸出:

    Output : NaN
  • 當“10”作為參數傳遞時。
    <script type="text/javascript"> 
       document.write("Output : " + Math.log(10)); 
    </script>

    輸出:

    Output : 2.302585092994046
  • 用不同的底數計算Math.log()。
    要找到以2為底的8的對數,請按以下方式執行math.log()函數:
    <script type="text/javascript"> 
       document.write("Output : " + Math.log(8)/Math.log(2)); 
    </script>

    輸出:

    Output : 3
  • 支持的瀏覽器:下麵列出了JavaScript Math.log()函數支持的瀏覽器:

    • 穀歌瀏覽器
    • IE瀏覽器
    • 火狐瀏覽器
    • Opera
    • 蘋果瀏覽器



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