0

Why am I getting these errors? (I have a clang/g++ compiler.)

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 inf and nan when they shouldn't
  • I suspected that this was due to complex numbers not being handled properly
  • This highly rated post notes that ccos, csqrt etc. should be used for complex arguments
  • I have tried various namespaces in addition to the above:
    • using complex;
    • using std::complex;
    • I tried also prepending std::complex and complex:: to each function

Disclaimer This is my first c/c++ function so please ignore trivial issues unless directly related to the question

Community
  • 1
  • 1
Alexander McFarlane
  • 10,643
  • 9
  • 59
  • 100
  • `std::complex` is a class, not a namespace. `cpow` etc. You can use a [reference](http://en.cppreference.com/w/cpp/numeric/complex) to see that the C++ functions do not have the leading c and are in `std`. – chris Aug 02 '16 at 21:27
  • As per the OP, I have also tried not including that line. I'll edit the post as it's going to distract form the issue – Alexander McFarlane Aug 02 '16 at 21:29
  • I was referring to *I tried also prepending std::complex and complex:: to each function* – chris Aug 02 '16 at 21:30
  • That "highly rated post" is about a different language (C, not C++). – MSalters Aug 03 '16 at 12:59
  • Also, that denominator could be written as just `std::cbrt(2)`. That's the `c` of "cubic root", not complex. – MSalters Aug 03 '16 at 13:03
  • Thanks for the tip but to save any else time on improvements relating to the main function body: The numerator and denominator are part of 24 million scrambled polynomials so I've no intention of messing with the contents and the function is not limiting performance – Alexander McFarlane Aug 03 '16 at 15:57

1 Answers1

3

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.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480