Printing double variable contents
- by Adil
I tried following code snippet and output is surprising me:
#include <stdio.h>
#include <math.h>
int main()
{
double num;
unsigned char ch;
ch = 19;
num = 1.0E+20 ;
num += ch * 1.0E+18;
printf("E18 = %lf \n",num);
printf("E18 = %e \n",num);
num = 11.0E+21 ;
num += ch * 1.0E+19;
printf("E19 = %lf <------\n",num);
printf("E19 = %e <------\n",num);
num = 11.0E+22 ;
num += ch * 1.0E+20;
printf("E20 = %lf\n",num);
printf("E20 = %e\n",num);
num = 11.0E+23 ;
num += ch * 1.0E+21;
printf("E21 = %lf\n",num);
printf("E21 = %e\n",num);
num = 11.0E+24 ;
num += ch * 1.0E+22;
printf("E22 = %lf <------\n",num);
printf("E22 = %e <------\n",num);
return 0;
}
The output of the program:
E18 = 119000000000000000000.000000
E18 = 1.190000e+20
E19 = 11190000000000000524288.000000 <------
E19 = 1.119000e+22 <------
E20 = 111900000000000001048576.000000
E20 = 1.119000e+23
E21 = 1119000000000000044040192.000000
E21 = 1.119000e+24
E22 = 11189999999999999366660096.000000 <------
E22 = 1.119000e+25 <------
Why the data corrupted when printed while in exponent form its OK