std::chrono::round
rounds to the nearest even number when it is exactly between numbers instead of rounding away from 0.
std::chrono::round<std::chrono::seconds>(3500ms) // = 4
std::chrono::round<std::chrono::seconds>(4500ms) // = 4 (instead of 5)
(Side question: Why would they do that?). I was wondering if their is a normal rounding function for chrono, like std::round()
?
In practice, fair rounding is not very useful for engineering, graphics, DSP, etc...
You'll have to roll your own. Here is the standard rounding used in engineering. This will give you the most consistent rounding for engineering-related tasks.
auto rounded_to_nearest_s = std::chrono::floor<std::chrono::seconds>(time + 500ms);
Keep in mind that rounding is actually domain-dependent, you do not round the same way in engineering applications than in banking applications (where fair rounding comes from), for example. It is definitely not unusual to have to write your own rounding functions.