如何使用 JavaScript 检查数字是否为无穷大?
在 JavaScript 中,当我们将任何数字除以零时,我们可以得到无穷大值。此外,开发人员在编写计算结果为无穷大的数学表达式时可能会犯错误。因此,在对数学表达式的返回值执行任何运算之前,我们需要检查数字值是否有限。
在这里,我们将学习三种使用 JavaScript 检查数字是否为无穷大的方法。
将数字值与 Number.POSITIVE_INFINITY 和 Number.NEGATIVE_INFINITY 进行比较
在 JavaScript 中,Number 是一个包含与数字相关的不同属性和方法的对象。Number 对象的 POSITIVE_INFINITY 和 NEGATIVE_INFINITY 属性允许开发人员评估数字的正无穷大值和负无穷大值。
我们可以将数值与 Number.POSITIVE_INFINITY 和 Number.NEGATIVE_INFINITY 进行比较,以检查数字是否为无穷大。
语法
按照以下语法使用数字对象的 POSITIVE_INFINITY 和 NEGATIVE_INFINITY 属性。
if (num1 == Number.POSITIVE_INFINITY || num1 == Number.NEGATIVE_INFINITY) { // 数字是有限的 } else { // 数字不是有限的 }
在上述语法中,我们使用 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) { // 将数值与 Number.POSITIVE_INFINITY 和 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)) { // 数字是有限的 } else { // 数字求值为无限 }
参数
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; // 使用 isFinite 方法; 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 不是有限的。 } else { // num 是有限的 }
示例
下面的示例包含 evaluateNumber() 函数,当用户单击求值数字按钮时会调用该函数。evaluateNumber() 函数首先将数值转换为正数值,并将其与 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() 方法,该方法将数字作为参数并在评估数字后返回结果。但是,用户也可以使用其他方法,因为所有方法都是线性的。