Alexander McFarlane - 10 months ago 36

C++ Question

Why am I getting these errors? (I have a

`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'?

and so forth. I have declared my function as:

`#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);

}

I am trying to ensure that I am handling the imaginary variables correctly.

I have spent a long time looking at various posts but I have the following issues:

- The values are coming out as and
`inf`

when they shouldn't`nan`

- I suspected that this was due to complex numbers not being handled properly
- This highly rated post notes that ,
`ccos`

etc. should be used for complex arguments`csqrt`

- I have tried various namespaces in addition to the above:

`using complex;`

`using std::complex;`

- I tried also prepending and
`std::complex`

to each function`complex::`

Answer

You are using functions from the C header `complex.h`

, which you are not including. But the C++ header `complex`

provides overloads for `cos`

, `sin`

, `pow`

, etc^{*}. You should use these instead of their C counterparts.

```
#include <complex>
int main()
{
std::complex<double> c{1, 2};
auto c2 = pow(c, 2);
auto cc = sqrt(c2);
}
```

^{* Note that these are in the std namespace, but so is std::complex, so argument dependent lookup (ADL) allows you to invoke them without the namespace.}

Source (Stackoverflow)