Today I was playing with floating-point number in C
code likes:
float a = 0.0;
a += 0.8;
if(a == 0.800000)
printf("correct");
The if statement do not get executed and for this I printed the value of a which was 0.800000.
void main(){
float a=0.0f;
a=a+0.1f;
if(a==0.1f)
printf( "1correct");
a += 0.1f;
if(a==0.2f)
printf( "2correct");
a += 0.1f;
if(a==0.3f)
printf( "3correct");
a += 0.1f;
if(a==0.4f)
printf( "4correct");
if(a==0.5f)
printf( "5correct");
a += 0.1f;
if(a==0.6f)
printf( "6correct");
a += 0.1f;
if(a==0.7f)
printf( "7correct");
a += 0.1f;
if(a==0.8f)
printf( "8correct");
}
The following program prints output as :
1correct2correct3correct4correct5correct6correct
the statement 7correct and 8correct are not being printed out.
Any one please can explain Brief here!!