Complex constant i in C++?

Viewed 196

When writing C++ code, rather than:

double a, b;
...
std::complex<double> z = a + b * std::complex<double>(0, 1);

I would prefer to write something like:

std::complex<double> z = a + b * i;

I can see that C99 has macro I (https://en.cppreference.com/w/c/numeric/complex/I), but it can't be use with std::complex:

std::complex<double> z = a + b * I; // does not compile

Of course, I could define myself some constant for that purpose, but that constant must exist somewhere already in C++. What is it called?

2 Answers

Since C++14, you can use the complex literal ""i to specify (only) the imaginary part of a complex number. Thus, for i you can use 1i.

Related