I want to implement the complex exp, sin and cos function. The problem is the following:

For computing $\exp(z)$ I wanted to use $\exp(z) = \cos(z)+ i\cdot\sin(z)$. Then I first have to implement sin and cos.

For computing sin and cos I wanted to use the power series expansion

$\sin(z) = z - \dfrac{z^3}{3!}+\dfrac{z^5}{5!}-\dfrac{z^7}{7!}+\dots$

same for $\cos(z)$.

So I need to implement first how to compute $(x+iy)^n$. I thought I should use something like $z^n = (re^{i\varphi})^n$. The problem: I need the complex exp first.

Even if I use the power series expansion for exp I need the code for raising a complex number to a power. Where should I start?

I also thougt about $z^n = (re^{i\varphi})^n = r^n(\cos(\varphi n) +i\sin(\varphi n))$ (De Moivre's Formula) which allows me to compute the $n$-th power of $z$ without using exp. You should see the point here. I cant compute sin and cos without computing the $n$-th power of $z$.

  • 2
    $\begingroup$ You can use the addition formula for $\sin$ to get $\sin(z) = \sin(x+iy) = \sin(x)\cosh(y) + i \sinh(y)\cos(x)$ (I'm assuming you can compute $e^x$ and $\sin(x)$ for real $x$?) $\endgroup$
    – Winther
    Apr 15, 2018 at 20:43
  • 3
    $\begingroup$ I may be missing something obvious - why not simply use repeated multiplication? $\endgroup$ Apr 15, 2018 at 21:26
  • 1
    $\begingroup$ Cannot you use your programming environment's real-valued exp, sin and cos? If so, exp(x + iy) is simply the complex number with real part exp(x) cos(y) and imaginary part exp(x) sin(y), which you compute easily using the build-in real math facilities. Then, given the complex exp function, you can easily implement the complex sin, cos, and log functions. And, finally, z^w is exp(w log (z)). $\endgroup$ Apr 15, 2018 at 21:31
  • 2
    $\begingroup$ Your first equation has a typo. $\exp( \underline{\mathrm{i}} z) = \cos(z) + \mathrm{i}\,\sin(z)$. Or, I guess, $\exp(z) = \cos(z/\mathrm{i}) + \mathrm{i}\,\sin(z/\mathrm{i})$. $\endgroup$ Apr 15, 2018 at 21:35
  • 1
    $\begingroup$ @Arjihad Since you are computing the sum from the power series you already have to have a loop from, say, $0$ to $n$, so all you have to do is multiply by $z$ each iteration (or $z^2$ if you go in steps of $2$, of course). $\endgroup$ Apr 15, 2018 at 21:51

4 Answers 4


If you have $z=a+bi$, you can find $r$ and $\theta$ for $z$ using $$r=\sqrt{a^2+b^2} \quad \text{and} \quad \theta =\arctan(a,b)$$ where $\arctan(a,b)$ gives you an angle from $0$ to $2\pi$ by incorporating the sign of the value of $a$ and $b$. Then you can use the normal exponential since $r$ and $\theta$ are real numbers to obtain $$z^n = r^n \cos(n\theta)+ir^n\sin(n\theta)$$

  • 2
    $\begingroup$ Oh sure I forgot that cos$(n\varphi)$ is the real cos and not the complex one. Now I can implement that easily. Thank you $\endgroup$
    – Arji
    Apr 15, 2018 at 20:30
  • $\begingroup$ By $\arctan(a,b)$ don’t you just mean $\arg z$? $\endgroup$ Apr 15, 2018 at 20:57
  • $\begingroup$ Yes, I do. It is the atan_2 function in Java, so I thought it might be best to leave it in a similar form here. $\endgroup$ Apr 15, 2018 at 20:59
  • $\begingroup$ @Arjihad -- You don't need $r$ and $\theta$. You just need to be able to calculate the $\mathbb R$eal sine and cosine (and exp). $\exp(x+iy) = \exp(x)\exp(iy) = e^x(\cos y+i\sin y)$. You can use Real power series for those. $\endgroup$
    – mr_e_man
    Apr 15, 2018 at 21:20

You are very much putting the cart before the horse.

In the comments you express a hesitance to just use repeated multiplication to compute $z^n$. You cannot get away from the fact that, fundamentally, exponentiation is repeated multiplication, because that idea lies at the heart of any definition of complex exponentiation. A concept like complex exponentiation doesn't just spring out of nowhere, it's built out of smaller building blocks. If you want to implement things from scratch, at one point of another, those building blocks are going to have to show up in your code. Repeated multiplication is not something that you can avoid, because in some objective sense it's what complex exponentiation is.

You expressed a concern over how long it would take to compute $z^n$ for large $n$. But the same concern applies to computing the series expansion of $e^z$ - you'll have to compute many many terms to get good precision. And for larger and larger values of $z$, you'll need more and more terms to get the same level of precision.

You should use repeated multiplication to implement $z^n$, possibly with the "by squaring" optimization.

  • $\begingroup$ Unless you are cutting off the power series below as well as above, exponentiation by squaring is not helpful since you need $z^n$ for $n$ in a contiguous range. $\endgroup$
    – Ian
    Apr 16, 2018 at 0:04

As has been mentioned and should be mentioned again: any programming environment with respect for itself already has support for complex numbers of elementary functions and you don't want to implement this yourself for any serious applications unless you really know what you are doing (and not even then). That being said if you want to do this as an exericse for yourself then here is a list of how the usual elementary functions can be computed for a complex number $z=x+iy$ in terms of their real valued functions. I'm assuming you know how to compute $e^x,\sin(x),$ etc. for real $x$.

The multiplication of two complex numbers $z = x+iy$ and $w = x'+iy'$

$$zw = xx' - yy' + i(xy'+x'y)$$

Complex conjugation $$z^* = x - iy$$

Division by a complex number

$$\frac{z}{w} = \frac{zw^*}{|w|^2}$$

The norm of a complex number $$|z| = \sqrt{x^2 + y^2}$$

The argument of a complex number $$\arg(z) = \begin{cases} \arctan(\frac y x) &\text{if } x > 0, \\ \arctan(\frac y x) + \pi &\text{if } x < 0 \text{ and } y \ge 0, \\ \arctan(\frac y x) - \pi &\text{if } x < 0 \text{ and } y < 0, \\ +\frac{\pi}{2} &\text{if } x = 0 \text{ and } y > 0, \\ -\frac{\pi}{2} &\text{if } x = 0 \text{ and } y < 0, \\ 0 &\text{if } x = 0 \text{ and } y = 0. \end{cases}$$ where I choose to simply define $\arg(0) \equiv 0$ (this is usually undefined). With these functions you can express $z$ on polar form $$z = |z| e^{i\arg(z)}$$ The complex exponential follows from Eulers formula $$e^{z} = e^xe^{iy} = e^x\cos(y) + ie^x\sin(y)$$ Trigonometric functions follows from the addition formulas $$\cos(z) = \cos(x)\cosh(y) - i\sin(x)\sinh(y)$$ $$\sin(z) = \sin(x)\cosh(y) + i\cos(x)\sinh(y)$$ where $\cosh(x) = \frac{e^x+e^{-x}}{2}$ and $\sinh(x) = \frac{e^{x}-e^{-x}}{2}$ are the hyperbolic functions.

The principal branch of the complex logarithm can be computed as $$\text{Log}(z) = \log|z| + i \arg(z)$$

Unlike the real logarithm the complex one is multivalued so other valid choices are $\text{Log}_k(z) = \log|z| + i[\arg(z)+2\pi k]$ where $k$ is an integer $k$. This is called the $k$th branch of the logarithm.

Powers of complex numbers can be defined via the exponential and a logarithm as $$z^\alpha \equiv e^{\text{Log}(z)\alpha}$$


You do NOT want to use the power series expansion to compute sin and cos.

The first step is to do a range reduction, so that the parameter is in a restricted range such as $[0, \pi/4]$.

Then you should investigate ways of computing the restricted sin and cos.

cordic is one thing you might look up.

For serious computation, do not roll your own functions.

  • 1
    $\begingroup$ You should probably give a reason why OP shouldn't use power series computations. If they're doing a project purely on computing $\sin$ and $\cos$ for complex $z$, I think it would be perfectly appropriate for OP to try methods they're comfortable with even if they're less efficient :) $\endgroup$
    – Jam
    Apr 15, 2018 at 21:00
  • $\begingroup$ I never would have cared about engineering efficiency if I didn't have mathematical curiosity first. $\endgroup$
    – djechlin
    Apr 16, 2018 at 2:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .