2023如何使用 JavaScript 检查数字是否为无穷大?

 所属分类:web前端开发

 浏览:75次-  评论: 0次-  更新时间:2023-09-18
描述:更多教程资料进入php教程获得。 在 JavaScript 中,当我们将任何数...
更多教程资料进入php教程获得。

如何使用 JavaScript 检查数字是否为无穷大?

在 JavaScript 中,当我们将任何数字除以零时,我们可以得到无穷大的值。此外,开发人员在编写计算结果为无穷大的数学表达式时可能会犯错误。因此,在对数学表达式的返回值执行任何操作之前,我们需要检查该数值是否是有限的。

在这里,我们将学习三种使用 JavaScript 检查数字是否为无穷大的方法。

将数字值与 Number.POSITIVE_INFINITY 和 Number.NEGATIVE_INFINITY 进行比较

在 JavaScript 中,数字是一个包含与数字相关的不同属性和方法的对象。 Number 对象的POSITIVE_INFINITY NEGATIVE_INFINITY 属性允许开发人员评估数字的正无穷大值和负无穷大值。

我们可以将数值与Number.POSITIVE_INFINITY Number.NEGATIVE_INFINITY 进行比较,以检查是否number 的计算结果为 Infinity。

语法

按照以下语法使用数字对象的POSITIVE_INFINITY NEGATIVE_INFINITY 属性。

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

在上面的语法中,我们使用了 OR (||) 运算符来计算 if 语句中的多个条件。

示例

在此示例中,我们定义了两个具有不同值的数字。 num1 包含有限值,num2 包含无限值。 checkNumberIsFinite() 函数将数字值作为参数,并通过将数字与 POSITIVE_INFINITY NEGATIVE_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中的一个关键字,表示无穷大值。

我们可以将我们的数字与无穷大和-无穷大两者进行比较,或者取数字的绝对值并仅与无穷大进行比较。

语法

用户可以使用以下语法来使用 Math.abs() 方法和 Infinity 关键字来检查 number 的计算结果是否为 Infinity

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

示例

下面的示例包含evaluateNumber() 函数,该函数在用户单击评估数字按钮时调用。 evaulateNumber() 函数首先将数字值转换为正数值,并将其与 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() 方法,该方法将数字作为参数,并在计算数字后返回结果。然而,用户也可以使用其他方法,因为所有方法都是线性的。

 标签:
积分说明:注册即送10金币,每日签到可获得更多金币,成为VIP会员可免金币下载! 充值积分充值会员更多说明»

讨论这个素材(0)回答他人问题或分享使用心得奖励金币

〒_〒 居然一个评论都没有……

表情  文明上网,理性发言!