I am writing an application wherein I have to log the received data with timestamp in milliseconds. This is my code
struct timespec ts;
struct tm tt;
clock_gettime(CLOCK_REALTIME,&ts);
localtime_r(&ts.tv_sec,&tt);
double d=(tt.tm_hour*3600+tt.tm_min*60+tt.tm_sec)*1000.0+ts.tv_nsec/1000000.0;
float f1=(tt.tm_hour*3600+tt.tm_min*60+tt.tm_sec)*1000.0f+ts.tv_nsec/1000000.0f;
float f2=(tt.tm_hour*3600+tt.tm_min*60+tt.tm_sec)*1000.0f;
float f3=ts.tv_nsec/1000000.0f;
float f4=f2+f3;
printf("d=%lf\tf1=%f\tf2=%f\tf3=%f\tf4=%f\n",d,f1,f2,f3,f4);
This is a sample output that got printed,
d=84137605.580233 f1=84137608.000000 f2=84137000.000000 f3=605.580200 f4=84137608.000000
But here the outputs of double variable d and float variables f1 & f4 are different. Can somebody tell me the reason for this.