How to Assign Infinity to a Number in C++
In this article, you will learn about how to assign infinity to a number in C++ with different examples.
Introduction:
In C++, using floating-point data types like float and double often involves assigning Infinity to an integer. The unique value infinity represents an unbounded integer beyond the finite set of representable values. Both positive and negative Infinity are possible for this. Utilizing the constant INFINITY specified in the <cmath> header, you can assign Infinity to a number in C++.
- Add the Correct Header:Your C++ program must include the <cmath> header to use INFINITY. You can use #include <cmath> at the start of your code to include it.
- Use INFINITY:The INFINITY constant can be used to easily assign Infinity to a variable after the <cmath> header has been included. Positive and negative Infinity can be assigned as appropriate.
For example:double positive_infinity = INFINITY;
double negative_infinity = -INFINITY;
- Data Types:Floating point data types such as double or float are typically utilized with INFINITY. The right data type must be selected depending on your particular requirements.
Example 1:
Let's take an example to demonstrate how to assign infinity to a number using float datatype in C++:
#include<iostream> #include<limits> using namespace std; int main() { // Assigning the value of Infinity to Inf variable. float Inf = numeric_limits<float>::infinity(); // Converting the value to negative and assigning it to negative_Inf. float negative_Inf= Inf*-1; cout << "The value of Positive infinity is : " << Inf << endl; cout << "The value of Negative infinity is : " << negative_Inf << endl; return 0; }
Output:
The value of Positive infinity is: inf The value of Negative infinity is: -inf
Understanding the above code:
- In this example, infinity's value has been assigned using the float data type variable.
- The range of numeric_limits<float>:: The Inf variable receives its value from the Infinity() function.
- Subsequently, we allocated a negative infinity value to a variable of a float data type called negative_Inf by assigning it the value of Inf*-1, which is the opposite of the Inf value.
- Our final step was to print the positive and negative values of Infinity.
Example:
Let's take an example to demonstrate how to assign infinity to a number using double data type in C++:
#include<iostream> #include<limits> using namespace std; int main() { // Assigning the value of Infinity to Inf variable. double Inf = numeric_limits<double>::infinity(); // Converting the value to negative and assigning it to negative_Inf. double negative_Inf= Inf*-1; cout << "The value of Positive infinity is : " << Inf << endl; cout << "The value of Negative infinity is : " << negative_Inf << endl; return 0; }
Output:
The value of Positive infinity is: inf The value of Negative infinity is: -inf
Understanding the above code:
- The value of Infinity has been assigned using the double data type variable.
- The double-precision numeric limits: The value of Infinity is assigned to the Inf variable by the Infinity () function.
- Next, we gave the value of Inf*-1 i.e., the negative of the Inf value to a double data type variable called negative_Inf. In this manner, we were able to give a variable a negative infinity.
- Lastly, the values of positive and negative Infinity have been printed.
Using Infinity from the cmath library:
Another way to assign the infinity value to a variable in C++ is to use the INFINITY function. The int data type cannot have values assigned to the float and double data types only. You can find it in the C++ library's cmath module. Below is an explanation and implementation of this.
Example:
Let's take an example to demonstrate how to assign infinity to a number using float data type in C++:
#include<iostream> #include<cmath> using namespace std; int main() { // Assigning the value of Infinity to Inf variable. float Inf = INFINITY; float negative_Inf= Inf*-1; cout << "The value of Positive infinity is : " << Inf << endl; cout << "The value of Negative infinity is : " << negative_Inf << endl; return 0; }
Output:
The value of Positive infinity is: inf The value of Negative infinity is: -inf
Using double datatype:
Example:
Let's take an example to demonstrate how to assign infinity to a number using double datatype in C++:
#include<iostream> #include<cmath> using namespace std; int main() { double Inf = INFINITY; double negative_Inf= Inf*-1; cout << "The value of Positive infinity is : " << Inf << endl; cout << "The value of Negative infinity is : " << negative_Inf << endl; return 0; }
Output:
The value of Positive infinity is: inf The value of Negative infinity is: -inf
Conclusion:
In C++, assigning Infinity to an integer is a simple process, especially when working with floating-point data types. You can easily represent values outside of the finite range of representable numbers by utilizing the INFINITY constant and the <cmath> header. It is particularly helpful when working with mathematical computations, which produce results that are undefined or unbounded. C++ offers a practical method for working with these exceptional numbers, regardless of whether you need to assign positive, negative, or infinite. It is important to select the right data type (usually float or double) according to your requirements. C++'s ability to assign infinite is a vital feature for precisely modelling and managing mathematical concepts and calculations.