How to Assign Infinity to a Number in C++?

In this article, we will discuss how to assign infinity to a number in C++ with several methods. Before going to its implementation, we must know about the infinity.

What are Infinity and Negative Infinity?

Infinity is a value that results from diluting a positive integer by a very small positive value, or it can be a very large value that our system is unable to represent in 64 bits. Many C++ libraries, like cmath and limits, define the positive or unsigned infinity value. However, there is no accepted way to express the negative infinity in C++. There are several techniques in C++ to express negative Infinity, which we shall go through in more detail.

Use Negative of numeric_limits::infinity()

The data type for the numeric_limits:: The infinity() function is used to assign the infinite value to a variable in C++. It can be found in the C++ library's limits module. Following are the implementation and justification.

1. Using Float data type C++ Implementation

Output:

How to Assign Infinity to a Number in C++

Explanation:

The following explanation explains how to interpret the code above:

  • The variable with the value of Infinity has the float data type.
  • The float-based numeric_limits:: The Infinity() function sets the Inf variable's value to Infinity.
  • After that, we took a float data type variable named negative_Inf and gave it the value of Inf*-1, which is the opposite of the infinite value. In this way, we gave a variable a negative infinity.
  • Next, the values of both positive and negative infinity are printed.

2. Using double data type C++ Implementation

Output:

How to Assign Infinity to a Number in C++

Explanation:

The following steps can be used to understand the code above.

  • We can used the double data type to give the variable the value of Infinity.
  • The double-valued numeric_limits:: The Infinity() function sets the Inf variable's value to Infinity.
  • After that, we took a double data type variable named negative_Inf and gave it the value of Inf*-1, which is the opposite of the infinite value. In this way, we gave a variable a negative infinity.
  • Next, the values of both positive and negative infinity are printed.

3. Using Int data type C++ Implementation

Output:

How to Assign Infinity to a Number in C++

Explanation:

  • We can use a variable of the int data type to assign the value of Infinity.
  • The limitations of numeric_int:: When dealing with int data types, the infinity() function behaves differently. In the instance of positive infinity, it assigns the value of 0 to the Inf variable; thus, we have added 1 to the value to indicate the infinity's sign.
  • After that, we took a variable of the int data type called negative_Inf and gave it the value of Inf*-1, which is the inverse of the inf value. In this way, we gave a variable a negative value.
  • Finally, we have printed both the positive and negative values.

Use Infinity from cmath Library.

In C++, we can also use INFINITY to give a variable the value of infinity. It cannot assign values to the int data type but to the float and double data types. It can be found in the C++ library's cmath module. Following are the implementation and justification.

1. Using Float data type C++ Implementation

Output:

How to Assign Infinity to a Number in C++

Explanation:

The following steps can be used to understand the code above.

  • We can use a float data type variable to assign the value of Infinity.
  • The INFINITY gives the Inf variable the value of Infinity.
  • After that, we took a float data type variable named negative_Inf and gave it the value of Inf*-1, which is the opposite of the infinite value. In this way, we gave a variable a negative infinity.
  • Finally, the values of both positive and negative infinity are printed.

2. Using double data type C++ Implementation

Output:

How to Assign Infinity to a Number in C++

Explanation:

The following steps can be used to understand the code above.

  • We can use a double data type variable to assign the value of Infinity.
  • The INFINITY gives the Inf variable the value of Infinity.
  • After that, we took a double data type variable named negative_Inf and gave it the value of Inf*-1, which is the opposite of the infinite value. In this way, we gave a variable a negative infinity.
  • Finally, the values of both positive and negative infinity are printed.





Latest Courses