LoginSignup
0
0

More than 3 years have passed since last update.

C++11 chrono で float 精度で time_since_epoch の時刻をキャストしてはいけない

Posted at
float GetCurrentTimeInMilliSeconds()
{
  std::chrono::system_clock::time_point p = std::chrono::system_clock::now();

  std::chrono::milliseconds msecs = std::chrono::duration_cast<std::chrono::milliseconds>(p.time_since_epoch());
  return msecs.count();
}

のような感じで,

float start_t = GetCurrentTimeInMilliSeconds();
float end_t = GetCurrentTimeInMilliSeconds();

float diff = end_t - start_t;

のようにして計時(時間差分取得)すると, 現在の epoch time は大きすぎて(?) float 精度ではうまく表現できないようで大きくずれてします.

double を使うか, st::chrono::milliseconds などの chrono が用意する型を使って計算するのがよいです.

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0