I noticed unexpected behaviour and narrowed it down to conversion from one duration to another.
#include <chrono>
#include <iostream>
#include <cmath>
using sec = std::chrono::seconds;
using fsec = std::chrono::duration<float>;
using dsec = std::chrono::duration<double>;
template<typename T>
auto count_intervals(T time_jump) -> uint32_t
{
constexpr auto interval = sec(10);
std::cout << "[debug] " << time_jump.count() << std::endl;
std::cout << "[debug] " << interval.count() << std::endl;
std::cout << "[debug] " << time_jump.count() / interval.count() << std::endl;
printf("[debug] %.10f\n", time_jump.count());
printf("[debug] %.10f\n", time_jump.count() / interval.count());
const uint32_t k = std::ceil(time_jump.count() / interval.count());
std::cout << "[debug] " << k << std::endl;
return k;
}
int main()
{
using clock_t = std::chrono::system_clock;
using time_point_t = typename clock_t::time_point;
using std::chrono::duration_cast;
const auto start_point = time_point_t();
const auto a = start_point + sec(25);
const auto b = start_point + sec(55);
std::cout << count_intervals<fsec>(b - a) << std::endl;
std::cout << count_intervals<fsec>(duration_cast<fsec>(b - a)) << std::endl;
const auto distance = duration_cast<std::chrono::duration<uint32_t>>(b - a);
std::cout << count_intervals<fsec>(distance) << std::endl;
std::cout << count_intervals<dsec>(b - a) << std::endl;
}
I cannot figure out why std::ceil rounds up. It seems when I use dsec instead of fsec all is fine. The question is what is my mistake in case of float duration representation. I thought I use std::chrono::duration as intended but obviously that is not the case. How should I perform conversion to fsec correctly? In general I would like to have a fraction-of-a-second duration format.
output
[debug] 30
[debug] 10
[debug] 3
[debug] 30.0000019073
[debug] 3.0000002384
[debug] 4
4
[debug] 30
[debug] 10
[debug] 3
[debug] 30.0000019073
[debug] 3.0000002384
[debug] 4
4
[debug] 30
[debug] 10
[debug] 3
[debug] 30.0000000000
[debug] 3.0000000000
[debug] 3
3
[debug] 30
[debug] 10
[debug] 3
[debug] 30.0000000000
[debug] 3.0000000000
[debug] 3
3