Home  >  Article  >  Web Front-end  >  How to check if a number is infinity using JavaScript?

How to check if a number is infinity using JavaScript?

王林
王林forward
2023-09-09 14:45:031148browse

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

In JavaScript, when we divide any number by zero, we can get infinity value. Additionally, developers may make mistakes when writing mathematical expressions that evaluate to infinity. Therefore, before performing any operations on the return value of a mathematical expression, we need to check whether the value is finite.

Here we will learn three ways to check if a number is infinity using JavaScript.

Compare numeric values ​​to Number.POSITIVE_INFINITY and Number.NEGATIVE_INFINITY

In JavaScript, a number is an object that contains different properties and methods related to numbers. The POSITIVE_INFINITY and NEGATIVE_INFINITY properties of the Number object allow developers to evaluate the positive and negative infinity values ​​of a number.

We can compare the numeric value with Number.POSITIVE_INFINITY and Number.NEGATIVE_INFINITY to check if number evaluates to Infinity.

grammar

Use the POSITIVE_INFINITY and NEGATIVE_INFINITY properties of numeric objects according to the following syntax.

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

In the above syntax, we have used the OR (||) operator to evaluate multiple conditions in the if statement.

Example

In this example, we define two numbers with different values. num1 contains finite values ​​and num2 contains infinite values. The checkNumberIsFinite() function takes a numeric value as an argument and prints the message accordingly by comparing the number with POSITIVE_INFINITY and NEGATIVE_INFINITY, regardless of whether the number is finite or not.

<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> 

Use isFinite() method

isFinite() The method takes a numeric value as a parameter and returns a Boolean value depending on whether the number is finite. Here we will call the isFinite() method with the Number object as a reference to evaluate the number more robustly.

grammar

Users can use the isFinite() method according to the following syntax to check whether the number is infinite. We take the Number object as a reference and pass the numeric value as a parameter.

if (Number.isFinite(num1)) {
   
   // number is finite
} else {
   
   // number evaluates to infinite
} 

parameter

  • num1 - This is a number to be evaluated.

return value

  • It returns a boolean value depending on whether the number is finite or infinite.

Example

We use the isFinite() method as the condition of the if-else statement. The isFinite() method returns true or false based on the value we pass as parameter. Depending on the return value, control of program execution goes to the if or else block.

<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> 

Use Math.abs() method and Infinity keyword

Math.abs() method allows us to get the absolute value of any number. Infinity is a keyword in JavaScript that represents an infinite value.

We can compare our number to both infinity and -infinity, or take the absolute value of the number and compare it only to infinity.

grammar

Users can use the following syntax to use the Math.abs() method and the Infinity keyword to check whether the calculation result of number is Infinity.

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

Example

The example below contains the evaluateNumber() function, which is called when the user clicks the Evaluate Number button. evaulateNumber() The function first converts the numeric value to a positive value and compares it with the Infinity keyword.

<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>

The best way to check if a number is infinity is to use the isFinite() method, which takes a number as a parameter and returns the result after calculating the number. However, users can also use other methods since all methods are linear.

The above is the detailed content of How to check if a number is infinity using JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete