# How to compute the sine of a complex number in floating-point arithmetic?

What is the most efficient way to numerically compute the sine of a complex number?

Suppose I want to calculate the sine of a complex number a + bi on a computer. Suppose that a and b are both floating point numbers with a limited precision and I want to compute the real and complex parts of the sine of a + bi.

I'm sure that there are many ways to break this down, but supposing that I have functions available in a library that compute exponents and trig functions of real numbers, how can I go about computing the sine of a complex number?

• That should be provided by the standard library of the language. If not, $$\sin (a+ib) = \sin a \cos (ib) + \cos a \sin (ib) = \sin a \cosh b + i \cos a \sinh b$$ should be faster and more accurate than anything you write by hand unless you're an expert. – Daniel Fischer May 16 '14 at 17:35
• @DanielFischer, I think that should be posted as an answer. I'm working on a library in Javascript. I want it to be compatible with this new technology called asm.js (which most libraries aren't). Therefore, I needed to know how I can do this sort of computation. I think that your answer is probably best! – Daniel Allen Langdon May 16 '14 at 17:42

## 2 Answers

You can use the usual trigonometric formulas ($\cos(a+b)=\cos a \cos b-\sin a\sin b$, etc.), and you also need:

$$\cos ix=\mathrm{cosh}\, x$$

$$\sin ix=i \,\mathrm{sinh}\, x$$

They are direct consequences of the definition of $e^z$ (and then trigonometric and hyperbolic functions) as a series.

Thus, for example,

$$\cos(a+ib)=\cos a\,\mathrm{cosh}\,b-i\sin a\,\mathrm{sinh}\, b$$

• You mean $\sin ix = i$ sh $x$, surely? (And I would write $\sinh x$ for sh $x$.) – TonyK May 16 '14 at 17:43
• Yes, latex typo :-) – Jean-Claude Arbaut May 16 '14 at 17:44
• The usual abbreviation in the United States for the function $x \mapsto \frac12(e^x+e^{-x})$ is $\cosh x$, and similarly $x \mapsto \frac12(e^x-e^{-x})$ is $\sinh x$. – MJD May 16 '14 at 17:45
• @MJD I know, but I'm french :-) Ok, I'll change this, since it's the most common in programming languages, so the most likely to be known... – Jean-Claude Arbaut May 16 '14 at 17:47
• No need to change it; I was only adding my comment to help explain your French abbreviation. – MJD May 16 '14 at 18:02

$(1)$ You can always use the taylor series of the $\sin$ function namely, $$\sin(z) = z - \frac{z^3}{3!} + \frac{z^5}{5!} - \frac{z^7}{7!} + \cdots$$

$(2)$ You can also use the relationship $$e^{iz}=\cos(z)+i\sin(z),\ \sin(z)=\frac{e^{iz}-e^{-iz}}{2i}$$

• I am familiar with using the Taylor series. I thought that perhaps there was a more efficient way that would perhaps involve defining the real and imaginary parts of the answer in terms of real-valued trig functions or somesuch. I am doing this in a computer program that will compute the sine of many, many complex numbers, so I'm looking for the most efficient way. – Daniel Allen Langdon May 16 '14 at 17:34
• @Daniel: You are right $-$ Rod's answer is irrelevant and unhelpful. – TonyK May 16 '14 at 17:38
• @Rod: Now that's better! +1 – TonyK May 16 '14 at 17:42
• I wouldn't call it irrelevant. I certainly could use a Taylor expansion to do this sort of calculation. I just don't know how it would compare to other ways of doing the calculation. – Daniel Allen Langdon May 16 '14 at 20:18
• @Daniel: It's irrelevant because you told us that you already have the trig functions over $\mathbb R$ at your disposal. – TonyK May 18 '14 at 19:08