Complex Numbers
The functions for complex numbers have a fixed home in the <complex> module. Knowing that home is the first step in reasoning about any call that involves complex values.
Why Complex Numbers Need Functions Too
Many programs that work with numbers need to apply mathematical functions to their values. Once the values are complex numbers, a natural question follows: are complex numbers just a special notation, or can you actually compute with them the way you compute with everyday numbers? The short answer is that complex numbers are full participants in arithmetic, and the most common mathematical functions accept them as well. This article follows a single path: find the functions, predict what they return, and then work through the arithmetic by hand.
What do you think happens?
Can complex numbers be added, multiplied, and passed into mathematical functions the same way real numbers can?
Reveal answer
Yes, the usual arithmetic operations are supported for them
Complex numbers support the usual arithmetic operations, so they can be added, subtracted, multiplied, and divided. The most common mathematical functions are also supported for them.
Where the Complex Functions Live
The mathematical functions for complex numbers are provided in one module, named complex and written with angle brackets as the header <complex>. This is the single place the source names for them. When you need a mathematical function that operates on complex values, the complex version of that function lives in <complex>. You do not have to hunt through unrelated libraries to find it.
The functions for complex numbers have a fixed home in the <complex> module. Knowing that home is the first step in reasoning about any call that involves complex values.
Reasoning about a call is easiest when you follow the path step by step. Start with the argument you are passing in and note its type. Next, locate the function in the <complex> module, since that is the version designed for complex numbers. Then apply the rule that governs the return type. Each step narrows the question until the answer is determined by the rule alone.
The Return Type Rule
For each of the complex versions of these functions, the return type is the same as the argument type. A complex argument therefore produces a complex return type, so the argument type determines the result type. Because the rule is stated for every function in this group, you can apply it to any of them without memorizing each function separately.
- The argument z has type std::complex<double>, so it is a complex value.
- std::sqrt is taken from the complex module and returns a complex value, matching the argument type.
- std::exp follows the same rule, so its result is also complex.
Complex Numbers as Arithmetic Values
Every complex number has the form $a+bi$. The number $a$ is the real part, the number $b$ is the imaginary part, and $i$ is the imaginary unit with $i^{2} = -1$. Two complex numbers are combined by working with their parts: real parts are combined with real parts, and imaginary parts are combined with imaginary parts. Keeping this two-part view in mind makes every operation below easier to follow.
Worked Example: Multiplying and Dividing Two Values
Multiply and divide $3+4i$ by $1-2i$
Compute the product $(3+4i)(1-2i)$ and the quotient $(3+4i)/(1-2i)$.
- 1Identify the parts
For $3+4i$, the real part is $3$ and the imaginary part is $4$. For $1-2i$, the real part is $1$ and the imaginary part is $-2$.
- 2Expand the product
$(3+4i)(1-2i)=3-6i+4i-8i^{2}$. The imaginary terms combine to $-2i$.
- 3Replace i squared
With $i^{2}=-1$, the term $-8i^{2}$ becomes $+8$, so the product is $3+8-2i$.
- 4Multiply by the conjugate for the division
The conjugate of $1-2i$ is $1+2i$. Multiply the top and bottom by it: $(3+4i)(1+2i)/((1-2i)(1+2i))$.
- 5Simplify the denominator
$(1-2i)(1+2i)=1+4=5$, which is real. The numerator expands to $3+6i+4i+8i^{2}=-5+10i$.
- 6Divide each part
$(-5+10i)/5=-1+2i$.
The product is $11-2i$ and the quotient is $-1+2i$. Check the quotient by multiplying back: $(-1+2i)(1-2i)=3+4i$.
Common Mathematical Functions Accept Complex Values
Beyond the four arithmetic operations, the most common mathematical functions are also supported for complex numbers. A complex value can be passed into such a function, and the result is again interpreted as a complex quantity. The source introduces this idea briefly and does not list the individual functions, so the key takeaway is the general support rather than a catalogue. Functions such as sqrt and exp follow the return type rule described above, and the next section explains where learners often confuse them with abs.
Mistakes With Complex Functions and Types
Common mistake Searching for complex functions in an unrelated library
What it looks likeLooking for a complex square root in a general math header and assuming it is missing.
Why it failsThe mathematical functions for complex numbers are provided in the complex module, written as <complex>.
Do this insteadWhen you need a function that operates on complex values, look in <complex> first.
Common mistake Predicting the return value instead of the return type
What it looks likeTrying to work out the exact digits of a result before deciding what kind of value comes back.
Why it failsThe return type rule describes types, not the specific values the functions compute.
Do this insteadPredict the type first from the argument type, then compute the value separately.
Common mistake Assuming every function returns a complex value
What it looks likeExpecting abs applied to a complex number to return a complex number.
Why it failsabs is contrasted with the complex-result functions: it returns a real value, so its type differs from the complex argument passed in.
Do this insteadCheck the return type for each function rather than applying the complex rule blindly. The comparison below makes the difference visible.
Common mistake Forgetting that i squared equals -1 during multiplication
What it looks likeWriting $(2+3i)(1+i)$ as $2+2i+3i+3i^{2}$ and leaving $3i^{2}$ unchanged.
Why it failsThe term $bd\,i^{2}$ must be rewritten with $i^{2}=-1$, and this is the step where most mistakes happen.
Do this insteadReplace $i^{2}$ with $-1$ immediately, giving $2+5i-3=-1+5i$.
Common mistake Combining parts one at a time during multiplication
What it looks likeMultiplying only the real parts and only the imaginary parts separately.
Why it failsEach part of the first number interacts with each part of the second, so the product requires full expansion.
Do this insteadExpand all four terms, then simplify.
Common mistake Dividing without using the conjugate
What it looks likeTrying to divide $(3+4i)$ by $(1-2i)$ by dividing the real parts and the imaginary parts separately.
Why it failsDivision uses the conjugate of the denominator, which removes the imaginary unit from the denominator.
Do this insteadMultiply the top and bottom by $1+2i$, then simplify.
Practice Predicting Return Types
A program passes a complex value z into a function from the complex module. Without computing anything, state the return type. Then explain which step of the reasoning path you used to decide it.
Hints
- Start by noting the type of the argument: is it complex?
- Locate the function in the <complex> module.
- Apply the rule: the return type matches the argument type.
Compute $(2+3i)-(5-i)$ and $(2+3i)(5-i)$ by hand, using the parts of each number. Check your division answer by multiplying it back by the divisor.
Hints
- For subtraction, subtract real parts from real parts and imaginary parts from imaginary parts.
- For the product, expand all four terms and replace $i^{2}$ with $-1$.
Key Takeaways
- The mathematical functions for complex numbers are provided in the <complex> module, which is the single place to look for them.
- For these functions the return type matches the argument type, so a complex argument produces a complex result. The rule governs types, not specific values.
- Complex numbers support addition, subtraction, multiplication, and division by working with real and imaginary parts, using $i^{2}=-1$ and the conjugate of the denominator.
- Common mathematical functions also accept complex values, but abs is a reminder that the return type must be checked for each function.