Standard Mathematical Functions
The mathematical functions for complex numbers are provided in the <complex> module.
Why Complex Numbers Need 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 expect the needed functions to be hidden inside some unrelated library. They are not. The C++ standard provides versions of these mathematical functions designed specifically for complex numbers, and it gives them one fixed home and one consistent rule about the kind of value they return.
This article follows a simple path. You will learn where the complex versions live, how to trace a call from its argument to its result, where mathematical functions go beyond the standard set, and how to classify an unfamiliar function name so you know which reference to open. The goal is to reason about a call before you ever run it.
Locating the Complex Versions in <complex>
The mathematical functions for complex numbers are provided in the module named complex, 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, <complex> is where its complex version lives.
Notice the wording: the complex versions are the ones designed for complex numbers. The rest of this article uses that distinction. If your argument is a complex value, you look in <complex> for the version built for that kind of input, rather than assuming a general-purpose function will handle it correctly.
Tracing a Complex Call from Argument to Result
Reasoning about a call is easiest when you follow the path step by step. Each step narrows the question until the answer is determined by a rule rather than by memory. Work through the four steps below in order for any complex-number function call.
- 1Start with the argument you are passing in and note its type. For example, the argument might be a complex<float>, a complex<double>, or a complex<long double>.
- 2Locate the function in the <complex> module, since that is the version designed for complex numbers.
- 3Apply the rule: the return type matches the argument type.
- 4Conclude the type of the result. Because the rule is stated for every function in this group, you do not need to memorize each function separately.
Predicting the result type of a complex call
You are reading code that passes a complex<double> argument to a complex-number mathematical function. What return type should you expect?
- 1Note the argument type
The argument is complex<double>.
- 2Locate the function
Complex-number functions are provided in <complex>, so the function you need is its complex version in that module.
- 3Apply the type rule
The return type of each of these functions is the same as the argument type. The argument type is complex<double>, so the return type is complex<double>.
- 4Check the scope of the rule
The rule describes the kind of value returned. It does not tell you the numeric value the function computes, so you still need the function's own specification for that.
The call returns a complex<double>. The type is determined by the argument type alone.
The Type Rule and What It Does Not Promise
For each of these 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, it works as a single tool. You do not need a separate fact for each function. If you know the argument is complex, and you know the function is one of the complex-number versions, the return type follows.
Mistakes When Looking for Complex Functions
Common mistake Assuming a complex-number function is found in the same place as any general math function
Why it failsThe source places the complex versions in the complex module, written as the header <complex>.
Do this insteadWhen the argument is a complex value, look first in <complex> for the version designed for complex numbers.
Common mistake Predicting the numeric value of a result from the return type rule
Why it failsThe rule describes types, not the specific values the functions compute.
Do this insteadUse the rule to predict the return type, then consult the function's specification for its value.
Common mistake Expecting the return type to differ from the argument type
Why it failsFor each of these functions, the return type is the same as the argument type.
Do this insteadA complex<double> argument gives a complex<double> return type.
Three Regions Beyond the Standard Set
Once a program needs something more specialized, a natural question follows: is there more available, and where does it come from? C++ provides mathematical functions beyond the standard set, and they are not all in one list. The extra functions fall into distinct regions, and knowing the region tells you where to find the specification.
The first region is the standard set of mathematical functions, which is the baseline. The second is a small group of additional functions found in the cstdlib header. The third is the family of special mathematical functions, which C++ treats as a separate topic with its own ISO standard. Under the broader heading of C++ mathematical functions, these three regions relate as shown in the diagram below.
The diagram makes one distinction clear. The cstdlib functions are an addition to the standard picture, not a separate subject, so they remain part of the general mathematical toolkit. Special mathematical functions form a branch with their own governing document. That is why the source treats them as a distinct topic rather than a simple extension of the header.
Why the Special Functions Follow a Separate Standard
The special mathematical functions are covered by a separate ISO standard, which the source cites as C++Math, 2010. The practical consequence is that these functions are specified in their own document. A programmer looking for them should not expect them to be described in the same place as the everyday standard functions.
Classifying an Unfamiliar Function by Its Source
Suppose you are reading a program and meet a mathematical function you do not recognize. Instead of searching at random, ask three questions in sequence. Each answer sends you to a different reference, so the path below narrows the search with every step.
Holding this three-part map in mind lets you move from an unfamiliar function name to the right reference without guessing. Sorting a function into its region tells you where to find its specification, which is the whole point of the exercise.
Practice Predicting and Classifying
A program passes a complex<float> argument to a complex-number mathematical function. Predict the return type, and state which header you would check first to find the function's complex version.
Hints
- The return type matches the argument type for these functions.
- Complex-number versions of these functions are provided in the complex module.
You find a function in documentation that is not part of the standard mathematical functions and is not among the few extras in cstdlib. Which separate reference governs it, and what does that tell you about where its specification is written?
Hints
- Consider the third region in the classification sequence.
- Special mathematical functions are covered by a separate ISO standard.
Key Takeaways
- The mathematical functions for complex numbers are provided in the complex module, written as the header <complex>.
- For each of these complex-number 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.
- C++ mathematical functions fall into three regions: the standard set, a few additional functions in cstdlib, and special mathematical functions governed by a separate ISO standard (C++Math, 2010).
- Classifying a function by asking in sequence whether it is standard, cstdlib, or special tells you which reference holds its specification.