为什么无法识别 `ccos`、`csqrt`、`cpow`?
Why are `ccos`, `csqrt`, `cpow` not recognised?
为什么会出现这些错误? (我有一个 clang/g++
编译器。)
error: use of undeclared identifier 'ccos'
error: use of undeclared identifier 'csqrt'; did you mean 'sqrt'?
error: use of undeclared identifier 'cpow'; did you mean 'pow'?
等等。我已将我的函数声明为:
#include <complex>
double ghmc1AccNormalised( double ph, double r, double t, double th){
complex<double> numerator;
complex<double> denominator;
double E = exp(1);
numerator=-(cpow(E,t*((-2*r + r*ccos(th) + r* // ... etc
// what follows is 24MB of a horrible polynomial from Mathematica
...
denominator = cpow(2,0.3333333333333333) //... etc
return creal(numerator/denominator);
}
我正在努力确保正确处理虚变量。
我花了很长时间查看各种帖子,但遇到以下问题:
- 值显示为
inf
和 nan
,而它们不应该
- 我怀疑是复数处理不当造成的
- This highly rated post 指出
ccos
、csqrt
等应该用于复杂参数
- 除上述之外,我还尝试了各种命名空间:
using complex;
using std::complex;
- 我还尝试在每个函数前添加
std::complex
和 complex::
免责声明这是我的第一个c/c++
功能所以请忽略琐碎的问题,除非与问题直接相关
您正在使用 C 头文件 complex.h
中的函数,您没有包含这些函数。但是C++ header complex
provides overloads for cos
, sin
, pow
,等等*。您应该使用它们而不是它们的 C 对应物。
#include <complex>
int main()
{
std::complex<double> c{1, 2};
auto c2 = pow(c, 2);
auto cc = sqrt(c2);
}
* 请注意,它们位于 std
命名空间中,但 std::complex
也是如此,因此参数依赖查找 (ADL) 允许您在没有命名空间的情况下调用它们。
为什么会出现这些错误? (我有一个 clang/g++
编译器。)
error: use of undeclared identifier 'ccos'
error: use of undeclared identifier 'csqrt'; did you mean 'sqrt'?
error: use of undeclared identifier 'cpow'; did you mean 'pow'?
等等。我已将我的函数声明为:
#include <complex>
double ghmc1AccNormalised( double ph, double r, double t, double th){
complex<double> numerator;
complex<double> denominator;
double E = exp(1);
numerator=-(cpow(E,t*((-2*r + r*ccos(th) + r* // ... etc
// what follows is 24MB of a horrible polynomial from Mathematica
...
denominator = cpow(2,0.3333333333333333) //... etc
return creal(numerator/denominator);
}
我正在努力确保正确处理虚变量。 我花了很长时间查看各种帖子,但遇到以下问题:
- 值显示为
inf
和nan
,而它们不应该 - 我怀疑是复数处理不当造成的
- This highly rated post 指出
ccos
、csqrt
等应该用于复杂参数 - 除上述之外,我还尝试了各种命名空间:
using complex;
using std::complex;
- 我还尝试在每个函数前添加
std::complex
和complex::
免责声明这是我的第一个c/c++
功能所以请忽略琐碎的问题,除非与问题直接相关
您正在使用 C 头文件 complex.h
中的函数,您没有包含这些函数。但是C++ header complex
provides overloads for cos
, sin
, pow
,等等*。您应该使用它们而不是它们的 C 对应物。
#include <complex>
int main()
{
std::complex<double> c{1, 2};
auto c2 = pow(c, 2);
auto cc = sqrt(c2);
}
* 请注意,它们位于 std
命名空间中,但 std::complex
也是如此,因此参数依赖查找 (ADL) 允许您在没有命名空间的情况下调用它们。