In this guide, we will explore the various techniques in C++ to assign infinity to a numerical value. Prior to delving into the practical application of these methods, it is essential to have a clear understanding of the concept of infinity.
What are Infinity and Negative Infinity?
Infinity is a concept that is achieved by dividing a positive integer with an extremely small positive value, or it can be an exceptionally large value that exceeds the representation limit of a 64-bit system. Various C++ libraries such as cmath and limits provide definitions for positive or unsigned infinity. Nevertheless, there is no standardized method for representing negative infinity in C++. There exist several approaches in C++ for denoting negative infinity, which will be explored further.
Use Negative of numeric_limits::infinity
The data type for the numeric_limits:: The infinity method is employed to assign an infinite value to a variable in C++. This functionality is available within the limits module of the C++ library. Below details the implementation and rationale for its usage.
1. Using Float data type C++ Implementation
#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:
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
#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:
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
#include<iostream>
#include<limits>
using namespace std;
int main(){
// Assigning the value of Infinity to Inf variable.
int Inf = numeric_limits<int>::infinity() + 1;
// Converting the value to negative and assigning it to negative_Inf.
int negative_Inf= Inf*-1;
cout << "Positive value : " << Inf << endl;
cout << "Negative value : " << negative_Inf << endl;
return 0;
}
Output:
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 have the option to assign a variable the value of infinity using the keyword INFINITY. This feature is applicable to float and double data types but not to the int data type. INFINITY is included in the cmath module of the C++ standard library. Below, you will find the code implementation along with the rationale behind its usage.
1. Using Float data type C++ Implementation
#include<iostream>
#include<cmath>
using namespace std;
int main() {
// Assigning the value of Infinity to Inf variable.
float Inf = 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:
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
#include<iostream>
#include<cmath>
using namespace std;
int main() {
// Assigning the value of Infinity to Inf variable.
double Inf = 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:
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.