Use of <complex> and function matching

Hi all, can you help me figure out why this code doesn't work. The function call to exp() should call the function defined in the complex library. But I have to either give it a complex arg or explicitly convert it to a complex. I would think it would automatically convert to the correct type by matching the arg type.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  /*use of complex numbers. Euler's formula to compute exp(i*PI)=-1 */
#include <cmath>
#include <complex>
#include <iostream>
#include <iomanip>

const double PI {std::acos(-1)};

int main() {
           std::cout << std::fixed << std::setprecision(2);
           std::complex<double> z0,z1;
           z0 = 1i * PI;
           std::cout << "i * pi = " << z0 << '\n';
           z1 = std::exp(z0);                                   //works
//         z1 = std::exp(1i * PI);                              //ERROR!
           z1 = std::exp((std::complex<double>) 1i * PI);       //works
           std::cout << "exp(i * pi) = " << z1 << '\n';
           return 0;
}


I'm using g++ (GCC) 7.3.0 with no options.

thank you,
Scott
Try your code in C++ shell (gear-wheel icon to the top right of the code sample).

Your marked line is not the only problem with your code.

I think you may have been relying on a few non-standard compiler extensions.
Last edited on
I tried the C++ Shell and it does not compile. But I'm using GNU GCC. My code compiles under GNU GCC (under Ubuntu, MinGW/MSYS, and Cygwin).

My code does rely on complex_literals (in C++14), but these are recognized by default by the GCC version (except the older version uder Ubuntu, which requires -std=c++14 to compile). Otherwise I did not specify any compiler options.

But even if I explicitly give the command-line options, it still give the same error: it cannot find the function based on the arg list. It recognizes the literals, and it seems to be correctly converting the mixed data-type arg to a complex data type, but it seems to be unable to find the exp() function from the complex library:

1
2
3
4
5
6
g++ -std=c++14 -fext-numeric-literals -Wall test_complex.cpp
test_complex.cpp: In function ‘int main()’:
test_complex.cpp:16:26: error: no matching function for call to ‘exp(__complex__ double)’
     z1 = std::exp(1i * PI);   
                          ^

Last edited on
> I'm using g++ (GCC) 7.3.0 with no options.

Compile with -std=c++14 -Wall -Wextra -pedantic-errors

This is required for complex literals to be available: using namespace std::literals ;
More information: https://en.cppreference.com/w/cpp/numeric/complex/operator%22%22i

With these, the code works as expected: http://coliru.stacked-crooked.com/a/b18a1d7f760b399c
I thought I did that already, but I guess I missed it. Thank you!

Addendum: I think I found the source of my error. I found that if I include the namespace complex_literals in my code and give the command line option -fext-numeric-literals, then the compiler issues an error.

1
2
3
4
5
6
7
8
           using namespace std::complex_literals;

 g++ -std=c++14 -fext-numeric-literals   test_complex.cpp
test_complex.cpp: In function ‘int main()’:
test_complex.cpp:16:26: error: no matching function for call to ‘exp(__complex__ double)’
     z1 = std::exp(1i * PI);    //ERROR!

Last edited on
Topic archived. No new replies allowed.