如何在 JavaScript 中指定 Math.log() 的底數?


在本教程中,我們將學習如何計算任何數字的值的對數,併為 Math.log() 方法指定底數。

數學的對數運算在許多地方都有用。例如,如果要顯示數十億個值的圖形表示,則很難在螢幕上渲染。為此,我們使用對數更改圖形的比例,這樣就可以輕鬆地生成和視覺化任何特定操作的輸出。

我們還可以使用對數函式的其他許多示例。

  • 使用 Math.log() 計算對數

  • 為 Math.log() 指定不同的底數

  • 使用 Math.log10() 指定底數 10

使用 Math.log() 計算對數

在 JavaScript 中,Math.log() 是 Math 庫的內建方法,用於計算不同數值的自然對數。它以數值作為引數,並返回以 E 為底的數字的對數,這意味著自然對數。在數學語言中,我們可以說 Math.log() 返回 ln( number ) 的值。

語法

以下是 JavaScript 中使用 Math.log() 函式的語法。

let output = Math.log( number )

引數

  • number − 它是我們想要計算自然對數的值。

返回值

  • Math.log() 函式返回正數值的自然對數。對於 0,它返回 -Infinity,對於負數和非數值,它返回 NaN

  • 對於字串數字輸入,該函式會自動從字串中解析數字值並返回輸出。

示例

以下示例演示了使用不同輸入值的方法 Math.log()

<!DOCTYPE html>
<html>
<head>
   <title> Calculating the log using the Math.log(). </title>
</head>
<body>
   <h2> Calculate the natural logarithm using the Math.log() function </h2>
   <h4> Natural logarithm of 10000 </h4>
   <div id="output1"> </div>
   <h4> Natural logarithm of -165 </h4>
   <div id="output2"> </div>
   <h4> Natural logarithm of "223" </h4>
   <div id="output3"> </div>
   <h4> natural logarithm of "2ws" </h4>
   <div id="output4"> </div>
   <script type="text/javascript">
      let output1 = document.getElementById("output1");
      let output2 = document.getElementById("output2");
      let output3 = document.getElementById("output3");
      let output4 = document.getElementById("output4");
      var number = 10000;
      output1.innerHTML = Math.log(number);
      number = -165;
      output2.innerHTML = Math.log(number);
      number = "223"; // function automatically parses the number from string
      output3.innerHTML = Math.log(number);
      number = "2ws";
      output4.innerHTML = Math.log(number);
   </script>
</body>
</html>

在上面的輸出中,使用者可以看到,對於負數和非數值,Math.log() 函式返回 NaN。

為 Math.log() 指定不同的底數

在本節中,我們將學習如何為對數函式指定不同的底數。JavaScript 中沒有內建功能來更改對數的底數,但我們可以應用一些數學邏輯來更改底數。

在數學中,

$$\mathrm{\mathit{log_{b}}\left ( \mathit{X} \right )=\:\mathit{log_{nb}\left ( X \right )}/\mathit{log_{nb}\left ( b \right )}}$$

使用上述方法,我們可以更改數學中的對數底數。上述公式表明,如果使用者想要更改對數的底數,他們可以透過為兩者設定新的底數 (nb) 來將對數值除以底數的對數。在我們的例子中,我們將為兩個值設定自然對數底數 (e)。

語法

使用者可以按照以下語法更改底數。

let output = Math.log( number ) / Math.log( base );

示例

以下示例演示了在 JavaScript 中使用對數函式的不同底數。

<!DOCTYPE html>
<html>
<head>
   <title> Calculating the log using the Math.log(). </title>
</head>
<body>
   <h2> Calculate the logarithm of numbers with different bases </h2>
   <h4> Logarithm of 4000 with respect to base 40. </h4>
   <div id="output1"> </div>
   <h4> Logarithm of 4000 with respect to base 20. </h4>
   <div id="output2"> </div>
   <script type="text/javascript">
      let output1 = document.getElementById("output1");
      let output2 = document.getElementById("output2");
      var number = 4000;
      output1.innerHTML = Math.log(number) / Math.log(40);
      output2.innerHTML = Math.log(number) / Math.log(20);
   </script>
</body>
</html>

使用 Math.log10() 方法指定底數 10

Math.log10() 方法與 Math.log() 方法相同。兩種方法之間的區別在於 Math.log10() 方法使用 底數 10,而 Math.log() 方法使用 自然對數 (e) 作為底數。

語法

使用者可以按照以下語法使用 Math.log10() 方法。

Let output = Math.log10( number );

示例

使用者可以從以下示例中學習如何使用 Math.log10() 方法。

<!DOCTYPE html>
<html>
<head>
   <title> Calculating the log using the Math.log10(). </title>
</head>
<body>
   <h2> Calculate the logarithm of numbers using the Math.log10() Method</h2>
   <h4> Logarithm of 100000 with base 10 </h4>
   <div id="output1"> </div>
   <h4> Logarithm of 500000 with respect to base 10. </h4>
   <div id="output2"> </div>
   <script>
      let output1 = document.getElementById("output1");
      let output2 = document.getElementById("output2");
      var number = 100000;
      output1.innerHTML = Math.log10(number);
      number = 500000;
      output2.innerHTML = Math.log10(number);
   </script>
</body>
</html>

結論

本教程教會了我們如何更改 Math.log() 函式的底數。如果使用者想要將對數的底數更改為 10,則應使用第三種方法。對於不同的底數,使用者可以使用第二種方法。

更新於: 2022-07-14

668 次瀏覽

啟動您的 職業生涯

透過完成課程獲得認證

開始
廣告