Umang Mukesh Mehta Umang Mukesh Mehta - 2 months ago 21
C++ Question

Getting same value for timestamp on consequent calls

I am trying to get unix timestamp upto milliseconds in float whenever I call this function which is probably 30 times/second. The timestamp that the function below returns is always the same however, when I print it to

I get different value. I am creating the variable every time I call the function so I am not sure what I am doing wrong here. Any help will be appreciated.

float GetUnixTimestamps()
float milliseconds_since_epoch =

std::cerr << milliseconds_since_epoch << std::endl;
return milliseconds_since_epoch;

EDIT: As per the suggestion
I am using C++ 11 and need to compile this on both Windows x64, x86 and Linux (mainly Ubuntu and CentOS). I would like to get the current time in unix. I have already read the S.O posts 19555121 and 16177295 but the value still remains the same for the timestamps.


float's precision is not good enough to do what you want here. The current number of milliseconds since 1970-01-01 is about 1,478,020,169,728. The next representable value in a float after this is 1,478,020,300,800. That is about 131s (2min) later.

double will do here.