In C language, you can use the predefined constant INFINITY
to represent positive infinity. This constant is defined in the header file <math.h>
, and its usage is as follows:
#include <stdio.h>
#include <math.h>
int main() {
double x = 1.0 / 0.0; // positive infinity
double y = -1.0 / 0.0; // negative infinity
if (x == INFINITY) {
printf("x is infinity\n");
}
if( y == -INFINITY) {
printf("y is negative infinity\n");
}
return 0;
}
In the above code, the INFINITY
constant represents positive infinity, and the -INFINITY
constant represents negative infinity.
For floating-point division, if the divisor is 0.0
, the result can be positive infinity (positive number divided by 0.0), negative infinity (negative number divided by 0.0), or NaN
(0.0 divided by 0.0), rather than an error or exception.