公告:魔扣目录网为广大站长提供免费收录网站服务,提交前请做好本站友链:【 网站目录:https://www.morko.net 】, 免友链快审服务(50元/站),

点击这里在线咨询客服
新站提交
  • 网站:51998
  • 待审:31
  • 小程序:12
  • 文章:1030137
  • 会员:747

在 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() 方法,该方法将数字作为参数,并在计算数字后返回结果。然而,用户也可以使用其他方法,因为所有方法都是线性的。

      以上就是如何使用 JavaScript 检查数字是否为无穷大?的详细内容,更多请关注www.92cms.cn其它相关文章!

分享到:
标签:javascript 如何使用 数字 无穷大 检查
用户无头像

网友整理

注册时间:

网站:5 个   小程序:0 个  文章:12 篇

  • 51998

    网站

  • 12

    小程序

  • 1030137

    文章

  • 747

    会员

赶快注册账号,推广您的网站吧!
最新入驻小程序

数独大挑战2018-06-03

数独一种数学游戏,玩家需要根据9

答题星2018-06-03

您可以通过答题星轻松地创建试卷

全阶人生考试2018-06-03

各种考试题,题库,初中,高中,大学四六

运动步数有氧达人2018-06-03

记录运动步数,积累氧气值。还可偷

每日养生app2018-06-03

每日养生,天天健康

体育训练成绩评定2018-06-03

通用课目体育训练成绩评定