Code:
#include <stdio.h>
int main( )
{
float a=1.0;
long i;
for(i=0; i<100; i++)
{
a = a - 0.01;
}
printf("a = %e\n",a);
}
The output is: a = 6.591528745047981e-7 , not 0.0 as I expected!
But
Code:
#include <stdio.h>
int main( )
{
float a=1.0;
long i;
for(i=0; i<100; i++)
{
a = a - 0.01;
}
/* now reverse */
for(i=0; i<100; i++)
{
a = a + 0.01;
}
/* check if exact reversal occurred */
if (a==1.0)
{
printf("correct! a=%e\n",a);
/* why is a==1.0? */
}
else
{
printf("error! a = %e\n",a);
}
}
The output of this code is: correct! a = 1.0000000e + 000
I don't know why!
Please explain for me, thanks you very much!