gpt4 book ai didi

c++ - 除法和乘法 std::chrono::durations

转载 作者:搜寻专家 更新时间:2023-10-30 23:51:12 24 4
gpt4 key购买 nike

我正在使用 std::chrono::duration 计算随时间的一些变化,以获得微分和梯形积分的近似值。

我想在一个持续时间内执行一些常见的操作,但不幸的是没有这样做,可能是因为我没有正确理解 chrono。

using std::chrono::milliseconds;
using namespace std;

milliseconds eightms = milliseconds(8);
milliseconds fourms = milliseconds(4);
milliseconds twoms = milliseconds(eightms / fourms); //<-- why do I need this cast?
milliseconds twoms = eightms / fourms;

cout << "twoms = " << twoms.count() << " ms" << endl;

预期的输出是

twoms = 2 ms

如果我不使用上面的转换,我会得到这个神秘的编译器错误,当我再次将除法的结果转换为毫秒时,它会按预期工作。

dur_div_mult.cpp: In function ‘int main()’:
dur_div_mult.cpp:13:11: error: no match for ‘operator=’ (operand types are ‘std::chrono::milliseconds {aka std::chrono::duration<long int, std::ratio<1l, 1000l> >}’ and ‘std::__success_type<long int>::type {aka long int}’)
twoms = eightms/fourms;
^
In file included from dur_div_mult.cpp:2:0:
/usr/include/c++/5/chrono:274:12: note: candidate: std::chrono::duration<_Rep, _Period>& std::chrono::duration<_Rep, _Period>::operator=(const std::chrono::duration<_Rep, _Period>&) [with _Rep = long int; _Period = std::ratio<1l, 1000l>]
duration& operator=(const duration&) = default;
^

/usr/include/c++/5/chrono:274:12:注意:参数 1 没有从“std::__success_type::type {aka long int}”到“const std::chrono::”的已知转换时长 >&'

同时将 2ms * 4ms 与结果相乘并不像我预期的那样有效。

twoms = milliseconds(2);
fourms = milliseconds(4);
eightms = twoms * fourms;
cout << "eightms = " << eightms.count() << " ms" << endl;

预期输出为:

eightms = 8 ms

dur_div_mult.cpp: In function ‘int main()’:
dur_div_mult.cpp:18:21: error: no match for ‘operator*’ (operand types are ‘std::chrono::milliseconds {aka std::chrono::duration<long int, std::ratio<1l, 1000l> >}’ and ‘std::chrono::milliseconds {aka std::chrono::duration<long int, std::ratio<1l, 1000l> >}’)
eightms = twoms * fourms;
^
In file included from dur_div_mult.cpp:2:0:
/usr/include/c++/5/chrono:424:7: note: candidate: template<class _Rep1, class _Rep2, class _Period> constexpr std::chrono::duration<typename std::chrono::__common_rep_type<_Rep2, _Rep1>::type, _Period> std::chrono::operator*(const _Rep1&, const std::chrono::duration<_Rep, _Period>&)
operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d)
^
/usr/include/c++/5/chrono:424:7: note: template argument deduction/substitution failed:
/usr/include/c++/5/chrono: In substitution of ‘template<class _Rep1, class _Rep2, class _Period> constexpr std::chrono::duration<typename std::chrono::__common_rep_type<_Rep2, _Rep1>::type, _Period> std::chrono::operator*(const _Rep1&, const std::chrono::duration<_Rep, _Period>&) [with _Rep1 = std::chrono::duration<long int, std::ratio<1l, 1000l> >; _Rep2 = long int; _Period = std::ratio<1l, 1000l>]’:
dur_div_mult.cpp:18:23: required from here
/usr/include/c++/5/chrono:424:7: error: no type named ‘type’ in ‘struct std::common_type<long int, std::chrono::duration<long int, std::ratio<1l, 1000l> > >’
/usr/include/c++/5/chrono:414:7: note: candidate: template<class _Rep1, class _Period, class _Rep2> constexpr std::chrono::duration<typename std::chrono::__common_rep_type<_Rep1, _Rep2>::type, _Period> std::chrono::operator*(const std::chrono::duration<_Rep1, _Period1>&, const _Rep2&)
operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
^
/usr/include/c++/5/chrono:414:7: note: template argument deduction/substitution failed:
/usr/include/c++/5/chrono: In substitution of ‘template<class _Rep1, class _Period, class _Rep2> constexpr std::chrono::duration<typename std::chrono::__common_rep_type<_Rep1, _Rep2>::type, _Period> std::chrono::operator*(const std::chrono::duration<_Rep1, _Period1>&, const _Rep2&) [with _Rep1 = long int; _Period = std::ratio<1l, 1000l>; _Rep2 = std::chrono::duration<long int, std::ratio<1l, 1000l> >]’:
dur_div_mult.cpp:18:23: required from here
/usr/include/c++/5/chrono:414:7: error: no type named ‘type’ in ‘struct std::common_type<long int, std::chrono::duration<long int, std::ratio<1l, 1000l> > >’

显然我没有正确使用 std::chrono::duration,但我做错了什么?

最佳答案

<chrono>遵循 dimensional analysis rules 的严格子集.

A duration有一个时间单位。让我们暂时忽略时间单位的精度,将其统称为T。 .

标量( intdouble 等)根本没有单位。

如果将两个 durations 相乘一起这会给你 T 2 单位(但这不会编译,请继续阅读)。如果你划分两个 durations ,你得到一个标量:T 0。如果你乘以 duration通过标量,你得到 T 1(只是时间)。如果你划分一个 duration通过标量,您还可以得到 T 1(只是时间)。如果将标量除以 duration , 你得到 T -1,通常称为频率(但这也不会编译)。

<chrono>提供物理子集,其中结果可以表示为 T 1(duration)或T 0(标量)。其他指数是不允许的,会导致编译时错误。这不是因为他们错了,而是超出了这个库的范围。

eightms / fourms结果为标量 2 .

你的意图是:

milliseconds twoms = eightms / 4;

关于c++ - 除法和乘法 std::chrono::durations,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57327896/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com