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


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

Math.log10()是JavaScript中的內置函數,可提供任意數量的以10為底的對數的值。
用法:

Math.log10(p)

    參數:該函數接受單個參數p,該參數p是要計算以10為底的對數的任何數字。

    返回值:它返回任意數字以10為底的對數的值。

    例子:

Input : Math.log10(5)
Output: 0.6989700043360189

說明:
此處顯示的數字5的以10為底的對數的值為0.6989700043360189。


Input : Math.log10(10)
Output:1

讓我們看一下有關此函數的一些JavaScript代碼:

  • 示例1:
    <script> 
      // Different numbers are being taken 
      // as the parameter of the function. 
      document.write(Math.log10(1000) + "<br>"); 
      document.write(Math.log10(12) + "<br>"); 
      document.write(Math.log10(26) + "<br>"); 
      document.write(Math.log10(5)); 
    </script>

    輸出:

    3
    1.0791812460476249
    1.414973347970818
    0.6989700043360189
  • 示例2:
    <script> 
      // Taken parameter from 1 to 19 incremented by 3. 
      for (i = 1; i < 20; i += 3) { 
          document.write(Math.log10(i) + "<br>"); 
      } 
    </script>

    輸出:

    0
    0.6020599913279624
    0.8450980400142568
    1
    1.1139433523068367
    1.2041199826559248
    1.2787536009528289
    

    錯誤和異常:此函數的參數應始終為數字,否則它將返回NaN,即,當其參數作為字符串時,則不是數字。

  • 示例1:
    <script> 
      // Parameters for this function should always be a 
      // number otherwise it return NaN i.e, not a number 
      // when its parameter taken as string. 
      document.write(Math.log10("gfg")); 
    <script>

    輸出:

    NaN
    
  • 示例2:該函數的參數為​​複數時會出錯,因為它僅接受整數值作為參數。
    <script> 
      // Parametes can never be a complex number because 
      // it accept only integer value as the parameter. 
      document.write(Math.log10(1 + 2i));

    輸出:

    Error: Invalid or unexpected token
    

應用:每當我們需要以10為底的對數的值時,我們都會借助此函數。它的值在數學問題中需要多次。
讓我們看看此應用程序的JavaScript代碼:

  • 示例1:
    <script> 
      // taking parameter as number 14 and  
      //calculated in the form of function. 
      function value_of_base_10_logarithms_of_any_number() 
      { 
          return Math.log10(14); 
      } 
      document.write(value_of_base_10_logarithms_of_any_number()); 
    </script>

    輸出:

    1.146128035678238

支持的瀏覽器:下麵列出了JavaScript Math.log10()函數支持的瀏覽器:

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


相關用法


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