如何使用 JavaScript 檢查數字是否等於 Infinity(無窮大)?


在 JavaScript 中,當我們將任何數字除以零時,可能會得到無窮大值。此外,開發者在編寫數學表示式時也可能犯錯,導致結果為無窮大。因此,在我們對數學表示式返回的值執行任何操作之前,我們需要檢查數字值是否有限。

在這裡,我們將學習三種方法來檢查 JavaScript 中的數字是否等於無窮大。

將數值與 Number.POSITIVE_INFINITY 和 Number.NEGATIVE_INFINITY 進行比較

在 JavaScript 中,Number 是一個物件,包含與數字相關的各種屬性和方法。Number 物件的 POSITIVE_INFINITYNEGATIVE_INFINITY 屬性允許開發者評估數字的正無窮大和負無窮大值。

我們可以將數值與 Number.POSITIVE_INFINITYNumber.NEGATIVE_INFINITY 進行比較,以檢查該數字是否等於無窮大。

語法

請遵循以下語法來使用 Number 物件的 POSITIVE_INFINITYNEGATIVE_INFINITY 屬性。

if (num1 == Number.POSITIVE_INFINITY || num1 == Number.NEGATIVE_INFINITY) {
   
   // number is finite
} else {
   
   // number is not finite
}

在上面的語法中,我們使用了 OR (||) 運算子來評估 if 語句中的多個條件。

示例

在這個示例中,我們定義了兩個具有不同值的數字。Num1 包含有限值,而 num2 包含無窮大值。checkNumberIsFinite() 函式將數值作為引數,並透過將數字與 POSITIVE_INFINITYNEGATIVE_INFINITY 進行比較,相應地列印訊息,說明數字是有限的還是無限的。

<html>
<body>
   <p>Comparing the number value with the <i> Number.POSITIVE_INFINITY and Number.NEGATIVE_INFINITY </i> to check if number evaluates to Infinity.</p>
   <div id = "output"> </div>
   <script>
      let output = document.getElementById("output");
      let num1 = 23;
      let num2 = 12 / 0;
      function checkNumberIsFinite(num) {
         
         // compare the numerical value with the Number.POSITIVE_INFINITY //and Number.NEGATIVE_INFINITY
         if (
            num == Number.POSITIVE_INFINITY ||
            num == Number.NEGATIVE_INFINITY
         ) {
            output.innerHTML += "The num is finite and it's value is " + num1 + "<br/>";
         } else {
            output.innerHTML += "The num is not finite <br/>";
         }
      }
      checkNumberIsFinite(num1);
      checkNumberIsFinite(num2);
   </script>
</body>
</html> 

使用 isFinite() 方法

isFinite() 方法將數值作為引數,並根據數字是否有限返回布林值。在這裡,我們將透過引用 Number 物件來呼叫 isFinite() 方法,以便更穩健地評估數字。

語法

使用者可以遵循以下語法使用 isFinite() 方法來檢查數字是否等於無窮大。我們以 Number 物件為參考,並將數值作為引數傳遞。

if (Number.isFinite(num1)) {
   
   // number is finite
} else {
   
   // number evaluates to infinite
} 

引數

  • num1 − 要評估的數字。

返回值

  • 它根據數字是有限的還是無限的返回布林值。

示例

我們將 isFinite() 方法用作 if-else 語句的條件。isFinite() 方法根據我們作為引數傳遞的數值返回 true 或 false。程式執行的控制根據返回值轉到 if 塊或 else 塊。

<html>
<body>
   <h3>Using the <i>isFinite()</i> method to check if number evaluates to Infinity.</h2> 
   <div id = "output"> </div>
   <script>
      let Output = document.getElementById("output");
      let num1 = -93;
      let num2 = -12 / 0;
      
      // using the isFinite method;
      if (Number.isFinite(num1)) {
         Output.innerHTML += "The num1 is finite and its value is " + num1 + "<br/>";
      } else {
         Output.innerHTML += "The num1 is not finite <br/>";
      }
      if (Number.isFinite(num2)) {
         Output.innerHTML += "The num2 is finite and its value is " + num2 + "<br/>";
      } else {
         Output.innerHTML += "The num2 is not finite <br/>";
      }
   </script>
</body>
</html> 

使用 Math.abs() 方法和 Infinity 關鍵字

Math.abs() 方法允許我們獲取任何數字的絕對值。Infinity 是 JavaScript 中表示無窮大值的關鍵字。

我們可以將我們的數字與 Infinity 和 -Infinity 都進行比較,或者取數字的絕對值,只與 Infinity 進行比較。

語法

使用者可以使用以下語法使用 Math.abs() 方法和 Infinity 關鍵字來檢查數字是否等於 Infinity

let number = Math.abs(num);
if (number == Infinity) {
   
   // num is not finite.
} else {
   
   // num is finite
}

示例

下面的示例包含 evaluateNumber() 函式,當用戶單擊“評估數字”按鈕時會呼叫該函式。evaluteNumber() 函式首先將數值轉換為正數值,並將其與 Infinity 關鍵字進行比較。

<html>
<body>
   <h3>Using the <i> Math.abs() method and Infinity keyword </i> to check if number evaluates to Infinity.</h3>
   <div id = "output"> </div><br>
   <button onclick = "evaluateNumber(23324/0)"> Evaluate number </button>
   <script>
      let output = document.getElementById("output");
      function evaluateNumber(num) {
         let number = Math.abs(num);
         if (number == Infinity) {
            output.innerHTML += "The number is not finite <br/>";
         } else {
            output.innerHTML += "The number is finite. <br/>";
         } 
      }
   </script>
</body>
</html>

檢查數字是否等於無窮大的最佳方法是使用 isFinite() 方法,該方法將數字作為引數,並在評估數字後返回結果。但是,使用者也可以使用其他方法,因為所有方法都是線性的。

更新於:2023年3月10日

466 次瀏覽

啟動你的 職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.