-
divide by zero
Code:
#include<stdio.h>
void main()
{
int i=60;int j=0;
j=i/j;
printf("%d\n",j);
}
this code was compiled with gcc 4.4.5 on Ubuntu, on execution an error message was thrown as "Floating point exception" But on changing datatype of j to float it produced "inf". I m not able to understand why it is not working same with j as int.
-
"inf" is defined as part of the IEEE float standard. IEEE 754-2008 - Wikipedia, the free encyclopedia
in other words the "inf" is not part of an int computer number standard.
Tim S
-
Because for IEEE floating point numbers, dividing a regular number by 0 yeilds inf.
For integers, dividing by 0 generally causes an interrupt. There is no standard result that is used.
-
If your compiler supports IEEE floating point, dividing by zero gives a "special" infinite value.
The C standard does not actually require that floating point values be represented using IEEE standard formats. There are real compilers that do not use IEEE formats (and I remember using one compiler that only used IEEE floating point if enabled using a command line switch).
Dividing by zero gives undefined behaviour according to the C standard. Which means any result is acceptable (reporting inf, crashing, etc). There is also no requirement for consistent behaviour between types.