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

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

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

What are Infinity and Negative Infinity?

Infinity is a value that comes when a positive number is divided by an extremely small positive value, or it can be a very large value that could not be represented in 64 bits by our system. The positive or unsigned infinity value is defined in many libraries in C++, like cmath and limits, but there is no standard for defining the negative Infinity in C++. But still, there are many ways to represent negative Infinity in C++, which we will discuss further.

Use Negative of numeric_limits::infinity()

The numeric_limits<(data type)>:: Infinity () function in C++ is used to assign the infinity value to a variable in C++. It is present in the limits module of the C++ library. The implementation and explanation are given below.

Using Float data type

C++ Implementation

Output

The above code can be understood in the following way:

  • We have used the float data type variable to assign the value of Infinity.
  • The numeric_limits< float >:: Infinity () function assign the value of Infinity to the Inf variable.
  • After that, we took a variable of float data type named negative_Inf, and then we assigned it the value of Inf*-1, which means the negative of the Inf value, so in this way, we have assigned a negative infinity to a variable.
  • Finally, we have printed the values of both positive and negative Infinity.

Using double data type

C++ Implementation

Output

In above code can be understood by the following steps.

  • We have used the double data type variable to assign the value of Infinity.
  • The numeric_limits< double >:: Infinity () function assign the value of Infinity to the Inf variable.
  • After that, we took a variable of double data type named negative_Inf, and then we assigned it the value of Inf*-1, which means the negative of the Inf value, so in this way, we have assigned a negative infinity to a variable.
  • Finally, we have printed the values of both positive and negative Infinity.

Using Int data type

C++ Implementation

Output

In above code can be understood by the following steps.

  • We have used the variable of int data type to assign the value of Infinity.
  • The numeric_limits< int >:: Infinity () function works differently in the case of int data type. It assigns the value of 0 to the Inf variable in the case of positive Infinity, so we have added 1 to the value to show the sign of the Infinity.
  • After that, we have taken a variable of int data type named negative_Inf, and then we have assigned it the value of Inf*-1, which means the negative of the Inf value, so in this way, we have assigned a negative value to a variable.
  • Finally, we have printed the values of both positive and negative values.

Use Infinity from cmath Library

The INFINITY in C++ can also be used to assign the infinity value to a variable. It can only assign values to the float and double data types but not to the int data type. It is present in the cmath module of the C++ library. The implementation and explanation are given below.

Using Float data type

C++ Implementation

Output

In above code can be understood by the following steps.

  • We have used the variable of float data type to assign the value of Infinity.
  • The INFINITY assigns the value of Infinity to the Inf variable.
  • After that, we took a variable of float data type named negative_Inf, and then we assigned it the value of Inf*-1, which means the negative of the Inf value, so in this way, we have assigned a negative infinity to a variable.
  • Finally, we have printed the values of both positive and negative Infinity.

Using double data type

C++ Implementation

Output

In above code can be understood by the following steps.

  • We have used the variable of double data type to assign the value of Infinity.
  • The INFINITY assign the value of Infinity to the Inf variable.
  • After that, we took a variable of double data type named negative_Inf, and then we assigned it the value of Inf*-1, which means the negative of the Inf value, so in this way, we have assigned a negative infinity to a variable.
  • Finally, we have printed the values of both positive and negative Infinity.

Conclusion

In this quick tutorial, we have discussed how to assign Infinity in C++. We can extract the following conclusions from the article -

  • The syntax and working of C++ infinity for different data types
  • Using functions from limits and cmath libraries to assign the value of Infinity.