Infinity in C
In this tutorial, we will discuss infinity in C. We will start with the discussion on infinity in general.
Next, we will discuss infinity as a run-time error in C and conclude with a discussion on storing and using infinity in C.
What is Infinity
We all know the number line from our elementary school. This number line is represented by arrows having no ending or beginning.
However, theoretically, we have both of them. The numbers terminate on positive ∞
(sometimes written as +∞
) infinity and start from negative infinity -∞
.
Every real number x
lies between – ∞ < x < ∞
. Infinity refers to a number whose weight is beyond any describable limit.
When we say we have infinite numbers, this means we have a non-ending amount of numbers.
We all know that when we divide a thing, we get pieces or parts of the actual thing. The smaller the divider, the more pieces we have.
To get more pieces, we have to divide the thing into smaller pieces. Therefore, if we divide something that approaches zero, we get infinite pieces (uncountable pieces).
Conversely, if we divide any real number by infinity, the result will be zero as it is the same as distributing a limited amount to infinite pieces; thereby, each piece will get nearly zero shares.
Infinity in Programming
Normally, programmers have a typical issue with infinity; whenever the divider is zero (usually by mistake) in an expression, it crashes the program (abnormal termination).
Abnormal termination can be a severe issue for life-critical software. The programmers never want an abnormal termination; thereby, the if-else
checks are used, or exception handling is performed.
Consider a simple code:
#include <stdio.h>
int main() {
printf("%d\n", 5 / 0);
printf("\nEnd of program\n");
return 0;
}
In this program, we are using a hardcoded division of 5
with 0
to check the program’s behavior; however, there can be a case where a programmer may use a variable as a divider whose value somehow becomes zero. We have tried it in Programiz C online compiler, and the output is below.
Floating point exception
We have tried the same code in another online compiler, and the output is below.
main.c: In function ‘main’:
main.c:3:23: warning: division by zero [-Wdiv-by-zero]
3 | printf ("%d\n", 5 / 0);
| ^
This program is terminated (crash) at the first print
statement. We have another print
statement, which is not executed because the program has already been terminated (called abnormal termination).
Representation of Infinity in C
In C, infinity is represented by INF
or inf
. Typically, it results from a division by zero or null value.
See this example code:
#include <math.h>
#include <stdio.h>
double divide(double d1, double d2) { return d1 / d2; }
int main() {
printf("Result: %lf\n", divide(2, 3));
printf("Result: %lf\n", divide(2, 0));
return 0;
}
There are two print
statements in the code. We call the function with non-zero parameters in the first print
statement, whereas the second parameter is zero in the second print
call.
Output:
Result: 0.666667
Result: inf
The situation may look similar to the first code; however, it is slightly different. In the first code, we have an expression (having divided by zero) within the print
statement, and as a result, the program is abnormally terminated.
However, this second program calls the divide
function from the main()
function. The divide
function returns inf
as a result of the call from the main
function, which prints it accordingly.
We can obtain the same results if we store the result in some variable in the main
function.
#include <math.h>
#include <stdio.h>
double divide(double d1, double d2) { return d1 / d2; }
int main() {
double result = divide(2, 0);
printf("Result: %lf\n", result);
return 0;
}
Besides getting inf
as a result, we can compare the result with infinity. Our next instructions may be based on the value of the expression, and many cases require not proceeding in case of the infinite result.
Therefore, we have to check the value before executing the next instructions.
For this purpose, we have a function isinf
to check whether the result is infinity or not. See the example:
#include <math.h>
#include <stdio.h>
double divide(double x, double y) { return x / y; }
int main() {
double res = divide(7, 3);
if (!isinf(res))
printf("Result is %lf\n", res);
else
printf("The result is infinity, and further computations not possible\n");
res = divide(7, 0);
if (!isinf(res))
printf("Result is %lf\n", res);
else
printf("The result is infinity, and further computations not possible\n");
return 0;
}
Notice the if
statement where we pass our result to the isinf
function to compare the result with infinity. If the res
has an infinite value, we skip the printf
statement.
Output:
Result is 2.33333
The result is infinity, and further computations not possible
It is evident from the output that we can’t proceed further in case of infinite results; however, the program is not doing any abnormal termination. In any case, the programmer can save any required data, can write logs, etc.
Negative Infinity in C
We also have negative infinity in C. For example, if we multiply infinity by a negative number, we will get negative infinity in the result.
See this simple code:
#include <math.h>
#include <stdio.h>
int main() {
double res = -1 * INFINITY;
printf("Result is %lf\n", res);
return 0;
}
The output of this code is Result is -inf
. Let’s look at another example to understand -INFINITY
manipulations.
#include <math.h>
#include <stdio.h>
double divide(double x, double y) { return x / y; }
int main() {
double res = divide(7, 3);
if (!isinf(res))
printf("Result is %lf\n", res);
else
printf("The result is infinity, and further computations not possible\n");
res = divide(-7, 0);
if (res != -INFINITY)
printf("Result is %lf\n", res);
else
printf(
"The result is negative infinity, and further computations not "
"possible\n");
return 0;
}
Here, the second function call to the divide
function asks it to divide a negative number by 0, thereby returning a value equal to negative infinity.
Output:
Result is 2.33333
The result is negative infinity, and further computations not possible
In this tutorial, we have covered both aspects. First, we discussed the methods to save our programs from abnormal termination in case of infinite results.
After that, we further looked at how our programs can further manipulate infinity to develop conditional expressions involving infinite values.