Operations on Complex Numbers
The complex header defines the usual mathematical functions for complex numbers, including a square root function and the pow() function for exponentiation.
Predicting a Complex Square
A complex number has two parts, a real part and an imaginary part, and ordinary arithmetic on it follows its own rules. When a program works with complex values, it often needs more than addition and multiplication. It also needs operations such as taking a square root or raising a value to a power. The C++ standard library supplies these operations for complex numbers, so programmers do not have to write the underlying math by hand each time.
What do you think happens?
If you multiply the complex number 1 + 2i by itself, what do you get?
Reveal answer
-3 + 4i
Expanding the product gives 1 + 2i + 2i + (2i)(2i). The last term is 4i^{2}, which equals -4 because i^{2} = -1. Adding the pieces gives 1 - 4 + 4i = -3 + 4i. Notice that the answer's real part is not simply 1^{2} + (2i)^{2}; the cross terms matter.
The Functions in the Complex Header
The complex header defines the usual mathematical functions for complex numbers. Two of them are the focus here: a square root function and the pow() function for exponentiation. The source names the square root function sqr_t(); in standard C++ it is called sqrt. Both functions belong to the same family of complex math functions, so once you learn how one is called and how its result is read, the others follow the same pattern.
The goal is to understand what each function is for, how its result should be interpreted, and where learners tend to go wrong. Before relying on a library function, it helps to know what answer it should give. Working the answer out by hand is the best way to check the output of a program.
Exponentiation as Repeated Multiplication
Raising a complex number to a power means multiplying it by itself a certain number of times. The source's central case is the power 2: raising $z = 1 + 2i$ to the power 2 means multiplying it by itself. For power 2, the result of pow() equals multiplying the base by itself. Higher powers extend the same idea, one multiplication at a time.
The diagram below builds a power step by step. Each arrow is one multiplication by z, and the value at each stage is the result of the previous stage multiplied by z again.
Each stage is a complex multiplication, so each result has its own real and imaginary parts. The second stage, $z^{2}$, is the same value that pow(z, 2) returns. The third stage carries the previous result forward and multiplies it by $z$ once more.
Expanding the Square by Hand
Squaring 1 + 2i
Compute $(1 + 2i)^{2}$ by multiplying the number by itself, and check whether the imaginary unit changes the real part.
- 1Write the product
Squaring means $(1 + 2i)(1 + 2i)$.
- 2Expand the four terms
Multiply each term of the first factor by each term of the second: $1 \cdot 1 + 1 \cdot 2i + 2i \cdot 1 + 2i \cdot 2i$.
- 3Simplify the terms
This gives $1 + 2i + 2i + 4i^{2}$, so the imaginary terms combine to $4i$.
- 4Apply i squared equals -1
Because $i^{2} = -1$, the term $4i^{2}$ becomes $-4$.
- 5Combine the real parts
The real parts are $1 - 4 = -3$, and the imaginary part is $4i$.
$(1 + 2i)^{2} = -3 + 4i$
Tracing the Cube of z
The same procedure extends to a higher power. Take the result of squaring and multiply it by z once more.
Multiplying the square by z again
Starting from $(-3 + 4i)$, multiply by $(1 + 2i)$ to find $z^{3}$.
- 1Write the product
$(-3 + 4i)(1 + 2i)$.
- 2Expand the terms
$-3 \cdot 1 + (-3) \cdot 2i + 4i \cdot 1 + 4i \cdot 2i$.
- 3Simplify
This gives $-3 - 6i + 4i + 8i^{2}$.
- 4Apply i squared equals -1
$8i^{2} = -8$, so the real part is $-3 - 8 = -11$.
- 5Combine the imaginary parts
$-6i + 4i = -2i$.
$z^{3} = -11 - 2i$
Square Roots Reverse the Question
The square root function is easiest to understand through a simple case. Since $i^{2} = -1$, the number $i$ is a square root of $-1$. Taking the square root of a complex number asks the reverse question: which complex value, when squared, gives the number you started with?
This reverse reading is useful for checking intuition. A square root of a negative real number is not a real number, which is exactly why a complex-number version of the square root function is needed. The input to sqrt is the number you want to take the root of, and the output is a complex value whose square is that input.
Calling the Functions in C++
The example below uses the same number, 1 + 2i, with both functions. The complex header must be included, and the functions are reached through the standard library. Each call returns a complex value, so the result can be stored in a complex variable and printed.
- The complex header provides both functions for complex numbers.
pow(z, 2)raises the base z to the power 2, which for power 2 equals multiplying z by itself.- sqrt(z) takes the square root; it is the function the source calls
sqr_t().
Before trusting the printed output, work out the expected value by hand, as in the worked example above, and compare it to what the program prints. For pow(z, 2) with z = 1 + 2i, the expected value is -3 + 4i.
Telling sqrt and pow Apart
The two functions answer opposite questions. The power function starts from a base and an exponent and produces a product. The square root function starts from a number and looks backward for the value that squares to it. The diagram below contrasts the two.
| Question | sqrt(z) | pow(z, 2) |
|---|---|---|
| Direction of the operation | Works backward to find a root | Works forward to build a product |
| What it takes as input | One complex number z | A base z and an exponent 2 |
| What it returns | A value w whose square is z | The value z multiplied by itself |
| Result for z = 1 + 2i | A value w with w times w equal to 1 + 2i | -3 + 4i |
The root and power functions answer different questions about the same input.
Because the two operations run in opposite directions, one cannot replace the other. Using pow(z, 2) when you wanted a square root gives you the square of z, not a value that squares to z, and the program will run without complaint while producing the wrong answer.
Mistakes Beginners Make
Common mistake Confusing sqrt() with pow()
What it looks likeCalling sqrt(z) when the goal is to multiply z by itself.
Why it failssqrt()looks backward for a value whose square is z.pow()moves forward and multiplies z by itself, so the two return different values.Do this insteadDecide first whether you need a root or a product, then choose the function that matches that direction.
Common mistake Squaring the real and imaginary parts separately
What it looks likeComputing $(1 + 2i)^{2}$ as $1^{2} + (2i)^{2} = 1 - 4 = -3$ and stopping there.
Why it failsThis drops the cross terms $2i \cdot 1$ and $1 \cdot 2i$, which add $4i$ to the result.
Do this insteadExpand the full product, including every cross term, and then apply $i^{2} = -1$.
Common mistake Forgetting that i squared is -1 when checking by hand
What it looks likeLeaving $2i \cdot 2i$ as an imaginary term instead of simplifying it.
Why it failsThe product $i^{2}$ is a real number, so the term changes the real part of the result.
Do this insteadWhenever a product contains $i^{2}$, replace it with $-1$ before combining terms.
Common mistake Assuming a program's output is correct without checking one case by hand
Practicing Both Operations
Let $z = 2 - i$. First, compute $z^{2}$ by expanding the product by hand. Then state which function in the complex header would produce this value, and explain why sqrt() could not be used in its place.
Hints
- Write $(2 - i)(2 - i)$ and expand all four terms.
- Remember that $(-i)(-i) = i^{2} = -1$.
- The answer should be $3 - 4i$, which
pow(z, 2)returns.
Key Takeaways
- The complex header provides a square root function and the
pow()function for exponentiation; the source names the square rootsqr_t(), which standard C++ calls sqrt. - For power 2,
pow(z, 2)equals multiplying z by itself, and higher powers come from repeated multiplication by z. - Expanding a complex product requires every cross term, and i squared equals -1, so the real part of $(1 + 2i)^{2}$ is -3, not 1 + (2i)^{2} without the cross terms.
- The square root asks which value, when squared, gives the input, while
pow()multiplies the base forward; the two cannot be substituted for one another. - Check a library result by working one case by hand before trusting the program's output.