Help us understand the problem. What is going on with this article?

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

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 が用意する型を使って計算するのがよいです.

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした