如何使用 JavaScript 检查数字是否等于 Infinity(无穷大)?
在 JavaScript 中,当我们将任何数字除以零时,可能会得到无穷大值。此外,开发者在编写数学表达式时也可能犯错,导致结果为无穷大。因此,在我们对数学表达式返回的值执行任何操作之前,我们需要检查数字值是否有限。
在这里,我们将学习三种方法来检查 JavaScript 中的数字是否等于无穷大。
将数值与 Number.POSITIVE_INFINITY 和 Number.NEGATIVE_INFINITY 进行比较
在 JavaScript 中,Number 是一个对象,包含与数字相关的各种属性和方法。Number 对象的 POSITIVE_INFINITY 和 NEGATIVE_INFINITY 属性允许开发者评估数字的正无穷大和负无穷大值。
我们可以将数值与 Number.POSITIVE_INFINITY 和 Number.NEGATIVE_INFINITY 进行比较,以检查该数字是否等于无穷大。
语法
请遵循以下语法来使用 Number 对象的 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 中表示无穷大值的关键字。
我们可以将我们的数字与 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() 方法,该方法将数字作为参数,并在评估数字后返回结果。但是,用户也可以使用其他方法,因为所有方法都是线性的。
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP