The C infinity is written as “INF” and it accrues in the outcome of dividing a positive numeric value by a null value or calculating a numeric value that is greater than the larger number of our system that can be represented in 64 bits. In the Institute of Electrical and Electronics Engineering (IEEE), the most famous binary demonstration of the numeric values here is the value of the positive infinity with all bits of the supportive set and all bits of the segment unoccupied. The style that is used to display an infinity is implementation-defined.

With the implementation of floating-point infinities, the infinity expands to the constant expression of float type data type, which evaluates to unsigned as well as positive infinity. On the other hand, the execution doesn’t care about floating infinities, and macro infinity enlarges the optimistic worth that insured to run-off a float data type at compile-time.

Assign Infinity value to a variable:

If we need infinity, use double or floating-point numeric values; you can easily get infinity. If the execution helps the double data-type infinities, the macro INFINITY develops to the continual appearance of type double, which estimates the positive and unidentified infinity. Look at this example:

#include

#include

float Calcula(double x, double y)

{

    if( x >y ) {return INFINITY; }

    else { /* your else if you have one */ }

}

int main()

{  

    double a=4.0, b=2.0;

    double ret;

    ret = isinf(Calcula(a, b));

    cout<<“Return value is %f”<<ret;

    return 0;

}

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/1-20.jpg" data-lazy- height="349" src="data:image/svg xml,” width=”636″>

We use a header file. ‘#include and #include which describe one macro and several type of mathematical functions. This library contains all functions which we need in the library that take return double as a result that was taken double as an argument and as you can see in the code. When the consequence of an object is not representable as a floating numeric value, we use the macro. In the main body, we initialized variables with the data type of the double having name “a”, “b”, and “ret” respectively. Then we assign a function of “isinf” to “ret” with the parameters of “a” and “b”. After that, we display the set.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/2-20.jpg" data-lazy- height="162" src="data:image/svg xml,” width=”632″>

Setting an int Infinity:

Integers are inherently finite; that is why we cannot define them to a right infinity. The nearby value that we can get is by initializing an “int” to its extreme value. The closest we can get by setting a variable to the maximum value that is double “a = std: numeric_limits:: max();”. Which would be 231-1 if it is 32 bits wide on our implementation. With this expression, we can assign this variable to the maximum value of int, which means it could be equal or greater than any other integer value.

#include

#include

using namespace std;

int main()

{  

    int a = std::numeric_limits<int>::infinity();

    cout<<“the value of a ist<<a;

    return 0;

}

}

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/3-18.jpg" data-lazy- height="235" src="data:image/svg xml,” width=”636″>

We integrate a header file #include and #include . These type of libraries defines the elements with the features of arithmetic types. More in detail, it describes a numeric limit class pattern and a specialization for individuals of the types. In the main body of the code, we initialized a variable with integer data-type, assign it a numeric limit with int, and apply infinity at the end of the statement. It is also called positive infinity because the value of ‘a’ equals zero. Then we display the result as we said before that integers are not finite, and we can’t set it to a true infinity.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/4-17.jpg" data-lazy- height="123" src="data:image/svg xml,” width=”632″>

Setting a float Infinity:

The only true infinities in C are the floating-point data-types if your platform supports the concept of a float; as we mentioned before, if we need infinity, use a floating-point number type, like float and double we will get infinity as it shows in the following example.

#include

#include

using namespace std;

int main()

{  

    float f = std::numeric_limits<float>::infinity();

    cout<<“the value of f ist<<f;

    return 0;

}

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/5-16.jpg" data-lazy- height="228" src="data:image/svg xml,” width=”633″>

First of all, we use two header files ‘#include and #include . Then we write the main body of the code, where we initialized a variable with float data-type and assigned it a numeric limit with float; you can also use double at the place of float and get infinity. We also apply infinity at the end by assigning the limit with float data type. Then we print the output, and you can see floating types are true infinities.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/6-16.jpg" data-lazy- height="161" src="data:image/svg xml,” width=”685″>

Negative Infinity:

The negative infinity result is less than zero so for the integer data-type, it does not have an infinity value rather we can use “std::numaric_limits:: infinity();” to get the maximum value of an integer.

#include

#include

using namespace std;

int main()

{  

    int b = std::numeric_limits<double>::infinity();

    cout<<“the value of b ist<<b;

    return 0;

}

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/7-12.jpg" data-lazy- height="223" src="data:image/svg xml,” width=”634″>

First of all, we wrote two header files ‘#include and #include . Then we start the main body of the code, where we initialized a variable with float data-type and assigned it a numeric limit with float; you can also use double at the place of float and get infinity. By assigning the limit with the float data type, we also apply infinity at the end. Then we print the output, and you can see that integer data types with double or float data types result in the negative limit of the integer value.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Screenshot-119.png" data-lazy- height="111" src="data:image/svg xml,” width=”611″>

Conclusion:

In this article, we define the infinity and the syntax of the infinity with the different data types. We discuss that infinity cannot be defined solely in terms of number or the combination of the numeric values with the examples of different types of infinity. We conclude that integer data type is not good for infinity, but float and double are the suitable data types for infinity.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/omar-150×150.png621ecdb53cc1e.jpg" height="112" src="data:image/svg xml,” width=”112″>

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.