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) {
// 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()方法以数字值作为参数,并根据数字是否有限返回布尔值。在这里,我们将通过以数字对象作为参考来调用isFinite()方法,从而更加稳健地评估数字。
语法
用户可以按照以下语法使用isFinite()方法来检查数字是否评估为Infinity。我们以数字对象作为参考,并将数字值作为参数传递。
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() 方法,该方法以数字作为参数并在评估数字后返回结果。然而,用户也可以使用其他方法,因为所有方法都是线性的。