Currently I am using boost::rational<std::uint64> to keep track in my application.
Basically I have a clock that runs over a very long period of time and will be tick by different components of different time resolutions, e.g. 1/50s, 1/30s, 1001/30000s etc… I want to maintain perfect precision, i.e. no floating point. boost::rational works well for this purpose, however I think it would be better design to use std::chrono::duration for this.
My problem though is, how can I use std::chrono::duration here? Since it uses a compile time period I don’t quite see how I can use it in my scenario where I need to maintain precision?
You’re allowed to set the period to
1and use a floating point type forRep.I suspect that you can do the same thing with
boost::rational, but you’ll have to look quite closely atstd::chrono, which I haven’t done. Look attreat_as_floating_pointandduration_values. Also try to figure out what the standard means by “An arithmetic type or a class emulating an arithmetic type”.One might reasonably argue that if
boost::rationaldoesn’t emulate an arithmetic type, then it’s not doing its job. But it doesn’t necessarily follow that it really does everythingstd::chrono::durationexpects.