chrono有不同的时间段? [英] chrono with different time periods?

查看:259
本文介绍了chrono有不同的时间段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我使用 boost :: rational< std :: uint64> 在我的应用程序中跟踪。



基本上,我有一个运行很长一段时间的时钟,并将由不同的时间分辨率的不同组件,例如1 / 50s,1 / 30s,1001 / 30000s等...我想保持完美的精度,即没有浮点。 boost :: rational 适用于此目的,但我认为这将是更好的设计使用 std :: chrono :: duration 为此。



我的问题是,我如何使用 std :: chrono :: duration 这里?因为它使用了一个编译时间段,我不知道如何在我需要保持精度的情况下使用它。

解决方案

您可以将期间设为 1 ,并为 Rep 使用浮点类型。 p>

可疑,您可以使用 boost :: rational 执行相同的操作,必须仔细观察 std :: chrono ,我没有这样做。查看 treat_as_floating_point duration_values 。还可以尝试通过算术类型或类仿真算术类型的类来确定标准的含义。



可能合理地认为如果 boost :: rational 不会模拟算术类型,那么它不会完成它的工作。但它不一定遵循它真的做的一切 std :: chrono :: duration 预期。


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 1 and use a floating point type for Rep.

I suspect that you can do the same thing with boost::rational, but you'll have to look quite closely at std::chrono, which I haven't done. Look at treat_as_floating_point and duration_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::rational doesn't emulate an arithmetic type, then it's not doing its job. But it doesn't necessarily follow that it really does everything std::chrono::duration expects.

这篇关于chrono有不同的时间段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆