std::chrono
ツールを使用して、いくつかの時差を測定しました。float 型と double 型の両方でテストを行いました。その結果には驚くべきことがありました。double 型は7 桁目以降に...0000000
移動しますが、float 型は有効桁数を表示し続けます。...9999999
コード:
std::chrono::steady_clock::time_point Start, End;
std::chrono::duration<float, std::ratio<1, 1>> fDuration;
std::chrono::duration<double, std::ratio<1, 1>> dbDuration;
std::cout.precision(20);
std::cout << std::fixed;
for (DWORD dwSleepTime=0; dwSleepTime<=10000; dwSleepTime+=1000)
{
Start = std::chrono::steady_clock::now();
Sleep(dwSleepTime);
End = std::chrono::steady_clock::now();
fDuration = std::chrono::duration_cast<std::chrono::duration<float>>(End - Start);
dbDuration = std::chrono::duration_cast<std::chrono::duration<double>>(End - Start);
std::cout << "Time passed (float) = " << fDuration.count() << std::endl;
std::cout << "Time passed (double) = " << dbDuration.count() << std::endl << std::endl;
}
その出力:
Time passed (float) = 0.00000000000000000000
Time passed (double) = 0.00000000000000000000
Time passed (float) = 1.00005722045898440000
Time passed (double) = 1.00005720000000010000
Time passed (float) = 2.00011444091796870000
Time passed (double) = 2.00011440000000020000
Time passed (float) = 3.00017166137695310000
Time passed (double) = 3.00017159999999980000
Time passed (float) = 4.00022888183593750000
Time passed (double) = 4.00022880000000040000
Time passed (float) = 5.00028610229492190000
Time passed (double) = 5.00028600000000000000
Time passed (float) = 6.00034332275390620000
Time passed (double) = 6.00034319999999970000
Time passed (float) = 7.00040006637573240000
Time passed (double) = 7.00040040000000020000
Time passed (float) = 8.00045776367187500000
Time passed (double) = 8.00045760000000070000
Time passed (float) = 9.00051498413085940000
Time passed (double) = 9.00051479999999950000
Time passed (float) = 10.00057220458984400000
Time passed (double) = 10.00057200000000000000
この現象の背後にある理由は何ですか? double 型は、この特定のケースで実際に精度の低い結果をもたらしますか?
私のシステム:
IDE: Visiual Studio 2012
OS: Windows 7 x64
CPU: Intel i5 750