在 C++ 中平均一组复数的正确方法是什么?
What is the proper way to average a set of complex numbers in c++?
我继承了一些需要对一些复数进行平均的 C++ 代码。给定一个集合,它将那些传递条件的值添加到累加器,然后除以添加的值的数量。
使用 gcc(当前为 gcc 8)如果我使用独立 operator/
,我会得到 template argument deduction/substitution failed
,但它与 std::complex::operator/=
一起工作正常。对于后续维护者来说,这样的细微差别似乎很脆弱。
那么求复数平均值的正确方法是什么?
并且该语言仅部分支持整型除法是否合理?
std::complex<double> A[32] = { /* initialization left to the reader */ };
int count = 0;
std::complex<double> sum;
for (auto i = 0; i < 32; ++i) {
if ( i % 2 ) { // arbitrary condition, not important
sum += A[i];
++count;
}
}
auto avg = sum / count; // this is ambiguous
sum /= count; // this is not
(代表错误,供好奇)
main.cpp: In function ‘int main()’:
main.cpp:16:16: error: no match for ‘operator/’ (operand types are ‘std::complex<double>’ and ‘int’)
auto avg = sum / count; // this is ambiguous
~~~~^~~~~~~
In file included from main.cpp:2:0:
/usr/include/c++/7/complex:434:5: note: candidate: template<class _Tp> std::complex<_Tp> std::operator/(const _Tp&, const std::complex<_Tp>&)
operator/(const _Tp& __x, const complex<_Tp>& __y)
^~~~~~~~
/usr/include/c++/7/complex:434:5: note: template argument deduction/substitution failed:
main.cpp:16:18: note: mismatched types ‘const std::complex<_Tp>’ and ‘int’
auto avg = sum / count; // this is ambiguous
^~~~~
In file included from main.cpp:2:0:
/usr/include/c++/7/complex:425:5: note: candidate: template<class _Tp> std::complex<_Tp> std::operator/(const std::complex<_Tp>&, const _Tp&)
operator/(const complex<_Tp>& __x, const _Tp& __y)
^~~~~~~~
/usr/include/c++/7/complex:425:5: note: template argument deduction/substitution failed:
main.cpp:16:18: note: deduced conflicting types for parameter ‘const _Tp’ (‘double’ and ‘int’)
auto avg = sum / count; // this is ambiguous
^~~~~
In file included from main.cpp:2:0:
/usr/include/c++/7/complex:416:5: note: candidate: template<class _Tp> std::complex<_Tp> std::operator/(const std::complex<_Tp>&, const std::complex<_Tp>&)
operator/(const complex<_Tp>& __x, const complex<_Tp>& __y)
^~~~~~~~
/usr/include/c++/7/complex:416:5: note: template argument deduction/substitution failed:
main.cpp:16:18: note: mismatched types ‘const std::complex<_Tp>’ and ‘int’
auto avg = sum / count; // this is ambiguous
^~~~~
不确定为它做除法的最正确方法是什么,但如果你仔细查看所有参数 operator/
和 operator/=
,你会注意到:
operator/
接受 std::complex<T>
或 T
.
- 因此,如果您使用
double
创建了原始复合体,那么它应该只接受另一个值,即 double
或 complex<double>
。
- 其实取决于你使用的编译器,
clang
直接给了invalid operands to binary expression ('std::complex<double>' and 'int')
错误
operator/=
接受std::complex<T>
或T
,同时也有float
、double
和[=25的特化=].
- 这意味着传递给
operator/=
的任何内容都将被隐式转换为 float
、double
或 long double
,而 int
可以做。
我继承了一些需要对一些复数进行平均的 C++ 代码。给定一个集合,它将那些传递条件的值添加到累加器,然后除以添加的值的数量。
使用 gcc(当前为 gcc 8)如果我使用独立 operator/
,我会得到 template argument deduction/substitution failed
,但它与 std::complex::operator/=
一起工作正常。对于后续维护者来说,这样的细微差别似乎很脆弱。
那么求复数平均值的正确方法是什么?
并且该语言仅部分支持整型除法是否合理?
std::complex<double> A[32] = { /* initialization left to the reader */ };
int count = 0;
std::complex<double> sum;
for (auto i = 0; i < 32; ++i) {
if ( i % 2 ) { // arbitrary condition, not important
sum += A[i];
++count;
}
}
auto avg = sum / count; // this is ambiguous
sum /= count; // this is not
(代表错误,供好奇)
main.cpp: In function ‘int main()’:
main.cpp:16:16: error: no match for ‘operator/’ (operand types are ‘std::complex<double>’ and ‘int’)
auto avg = sum / count; // this is ambiguous
~~~~^~~~~~~
In file included from main.cpp:2:0:
/usr/include/c++/7/complex:434:5: note: candidate: template<class _Tp> std::complex<_Tp> std::operator/(const _Tp&, const std::complex<_Tp>&)
operator/(const _Tp& __x, const complex<_Tp>& __y)
^~~~~~~~
/usr/include/c++/7/complex:434:5: note: template argument deduction/substitution failed:
main.cpp:16:18: note: mismatched types ‘const std::complex<_Tp>’ and ‘int’
auto avg = sum / count; // this is ambiguous
^~~~~
In file included from main.cpp:2:0:
/usr/include/c++/7/complex:425:5: note: candidate: template<class _Tp> std::complex<_Tp> std::operator/(const std::complex<_Tp>&, const _Tp&)
operator/(const complex<_Tp>& __x, const _Tp& __y)
^~~~~~~~
/usr/include/c++/7/complex:425:5: note: template argument deduction/substitution failed:
main.cpp:16:18: note: deduced conflicting types for parameter ‘const _Tp’ (‘double’ and ‘int’)
auto avg = sum / count; // this is ambiguous
^~~~~
In file included from main.cpp:2:0:
/usr/include/c++/7/complex:416:5: note: candidate: template<class _Tp> std::complex<_Tp> std::operator/(const std::complex<_Tp>&, const std::complex<_Tp>&)
operator/(const complex<_Tp>& __x, const complex<_Tp>& __y)
^~~~~~~~
/usr/include/c++/7/complex:416:5: note: template argument deduction/substitution failed:
main.cpp:16:18: note: mismatched types ‘const std::complex<_Tp>’ and ‘int’
auto avg = sum / count; // this is ambiguous
^~~~~
不确定为它做除法的最正确方法是什么,但如果你仔细查看所有参数 operator/
和 operator/=
,你会注意到:
operator/
接受std::complex<T>
或T
.- 因此,如果您使用
double
创建了原始复合体,那么它应该只接受另一个值,即double
或complex<double>
。 - 其实取决于你使用的编译器,
clang
直接给了invalid operands to binary expression ('std::complex<double>' and 'int')
错误
- 因此,如果您使用
operator/=
接受std::complex<T>
或T
,同时也有float
、double
和[=25的特化=].- 这意味着传递给
operator/=
的任何内容都将被隐式转换为float
、double
或long double
,而int
可以做。
- 这意味着传递给