C++ Mathematical Functions Overview
The mathematical functions for complex numbers are provided in the <complex> module.
Why Complex Numbers Get Their Own Functions
Many programs that work with numbers need to apply mathematical functions to their values. When those values are complex numbers, you might wonder whether the functions you need are hidden inside some separate, unrelated library. They are not. The C++ standard provides versions of these mathematical functions designed specifically for complex numbers, and it gives them a fixed home and one consistent rule about the results they hand back.
What do you think happens?
You pass a complex<double> argument to one of the complex-number mathematical functions. What type do you think the function returns?
Reveal answer
complex<double>
For each of these functions, the return type is the same as the argument type. A complex argument therefore produces a complex return type, and the argument type alone determines the result type. The rule describes the kind of value that comes back, not the particular number the function computes.
Following a Call from Argument to Result
Reasoning about a call is easiest when you follow the path one step at a time. The source describes three steps. First, start with the argument you are passing in and note its type. Second, locate the function in the <complex> module, because that is the version designed for complex numbers. Third, apply the rule that the return type matches the argument type. Each step narrows the question until the answer is determined by the rule alone, before the call is ever run.
Where the Complex-Number Functions Live
The versions of the mathematical functions that work on complex numbers are found in the module named complex, written with angle brackets as the header <complex>. When you need a mathematical function that operates on complex values, <complex> is where its complex version lives.
The source names <complex> as the single place to look for these functions. General-purpose mathematical functions belong to a different group, and the complex-number versions are the ones you look for in <complex>. Knowing this home for the complex versions lets you find them directly instead of searching through unrelated material.
- The <complex> header provides the complex number types used with the complex-number functions.
- complex<float>, complex<double>, and complex<long double> are the three argument types the source names.
- Declaring the variables does not call any mathematical function; it only sets up arguments whose types you can then reason about.
The Return Type Rule
For each of the complex-number 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.
Tracing a Call Through the Rule
Predicting the Return Type of a Complex-Number Call
A program passes a complex<float> argument to one of the complex-number mathematical functions in the <complex> module. What is the return type?
- 1Identify the argument type
The argument is complex<float>. Note this before anything else, because the rule depends on it.
- 2Locate the function
Because the argument is complex, the function is the complex version, which lives in the <complex> module.
- 3Apply the rule
The return type matches the argument type. The argument type is complex<float>, so the return type is complex<float>.
- 4Check what the rule does not cover
The rule gives the type only. It does not say which numeric value the result holds, so do not infer a value from it.
The return type is complex<float>. The same steps with a complex<double> argument give complex<double>.
Mapping Input Types to Output Types
The mapping works the same way for every complex argument type the source names. Each input type maps to an output type identical to it. Tracing the mapping from input to output means reading the argument type first and then copying that type across the call, not computing anything new.
| Argument type | Return type from the rule | Reasoning step |
|---|---|---|
| complex<float> | complex<float> | Return type matches argument type |
| complex<double> | complex<double> | Return type matches argument type |
| complex<long double> | complex<long double> | Return type matches argument type |
Predicted return types for the three complex argument types named in the source
Mistakes When Locating and Reasoning About Them
Common mistake Looking for the complex-number functions in the wrong place
What it looks likeSearching through general-purpose mathematical material when the goal is to work with complex values.
Why it failsThe source states that the complex versions of these functions are provided in the <complex> module, which is the single place named for them.
Do this insteadWhen the values are complex numbers, start in the <complex> module.
Common mistake Assuming the return type is something other than the argument type
What it looks likeExpecting a complex<double> argument to produce a plain double result.
Why it failsFor each of these functions the return type is the same as the argument type, so a complex argument produces a complex return type.
Do this insteadRead the argument type first and carry it straight across to the return type.
Common mistake Treating the return type rule as a rule about values
What it looks likeConcluding from the type rule what specific number a call will produce.
Why it failsThe rule describes types, not the specific values the functions compute.
Do this insteadUse the rule to predict the type, and consult the function itself for any claim about the computed value.
Checking Your Type Prediction
A program passes a complex<long double> argument to a complex-number mathematical function from the <complex> module. Without running the call, state the return type and explain which step of the reasoning path gives you the answer.
Hints
- Start by noting the argument type exactly as given.
- Ask which module holds the complex version of the function.
- Apply the rule that the return type matches the argument type.
Key Takeaways
- The mathematical functions for complex numbers are provided in the <complex> module, which is the single place the source names for them.
- For each of these functions, the return type is the same as the argument type, so a complex argument produces a complex return type.
- The return type rule describes types, not the specific values the functions compute.
- To predict a call's return type, note the argument type, locate the function in <complex>, and apply the rule that the return type matches the argument type.