2.5. Math Library: Functions and Constants

Time: 00:02:55 | Download: Large, Large (CC), Small | Streaming, Streaming | Slides (PDF)

Dennis Ritchie created the C programming language as the UNIX implementation language. Keeping the language small, partly by eliminating features not needed for an operating system, made the C compiler easier to port to different computer systems. Although modern cross compilers largely eliminate the porting advantages of a small language, the language architecture is now well established.

With this simple approach, neither C nor C++ implements an exponentiation operator. Nor do they provide any mathematical functions such as sine, cosine, tangent, or square root. While they don't implement these features as part of the language (i.e., they are not part of either language's syntax), they have always provided the functionality with library or API functions. The linker/loader scans each program object file for math library functions, extracting those needed from the math library and linking them with the executable program. Some older compilers require programmers to link the math library to their programs with the -lm command-line option.

The <cmath> Header File

Programs access the math library function declarations by including a header file: #include <cmath>. This header file also specifies some mathematical symbolic constants on some systems. M_PI, a symbolic constant for π, and the other mathematical constants are non-standard (i.e., not required by the ANSI C++ standard). Nevertheless, UNIX and Linux compilers typically support them.

The Symbolic Constant For π

Value Meaning Example
#define M_PI 3.14159265358979323846 π = M_PI a = M_PI * r * r;
M_PI. The cmath header file contains a 20-significant-digit approximation for π, making it easy to use and read in mathematical calculations. It is a common style, followed by C, C++, and Java, to name constants with all upper-case letters; furthermore, recall that C++ is a case-sensitive language, implying that M_PI must be spelled with all upper-case letters.

The cmath header provides many other non-standard math constants, and each one begins with M_, signifying its specification in the math header file.

#define _USE_MATH_DEFINES
#include <iostream>
#include <cmath>
using namespace std;
Visual Studio and math constants. The Microsoft Visual Studio compiler has an additional requirement for using the mathematical symbolic constants because they are non-standard. Programmers activate support for the constants in the VS compiler by putting the #define _USE_MATH_DEFINES directive at the top of the file, before any include directives.

Exponentiation (The pow Function)

Many languages, spreadsheets, etc., define an exponentiation operator, often using the caret or circumflex symbol for it: b^e. Alternatively, the FORTRAN and Python programming languages use two asterisks: b**e. C, C++, and Java take a different approach and provide this operation with a library math function named pow - a function that raises a number to a power.

Function Declaration Meaning Example
double pow(double b, double e) baseexponent = pow(base, exponent) a = M_PI * pow(r, 2);
The pow library function. The pow function has experienced a steady evolution from its introduction in C through several ANSI-standard revisions. Initially, its arguments and return type were type double. Later, additional functions with slightly varying names allowed other floating point types (e.g., float and long double ). C++ supports the various floating point types with a family of overloaded pow functions. If either argument is an integer, the compiler automatically promotes or converts it to a double before the function call. The pow function returns a floating point value whose size matches its widest argument, so the returned value is truncated if assigned to an integer. The data type does not appear in the function call.

Square Root (The sqrt Function)

Function Declaration Meaning Example
double sqrt (double x);  x  = sqrt(x) hypotenuse = sqrt(pow(a,2) + pow(b,2));
The sqrt library function. The sqrt function's evolution parallels the pow function, and the ANSI 2011 standard specifies several overloaded versions that handle other floating point data types. Like the pow function, programmers don't include the data type in the function call. In the example, a pow function call forms the sqrt function argument, illustrating that the arguments of both functions are appropriately valued (i.e., numeric) expressions.

Additional Math Functions

The ANSI 2011 standard specifies many other mathematical functions, including multiple versions of pow and sqrt that support more data types than just double. See <cmath> and select the "C++11" tab when looking at specific functions.

C++ Symbolic Math Constants

C++ defines many non-standard symbolic constants in <cmath> (or in the older C header file <math.h> ). Unless you write a lot of mathematical code, memorizing the name M_PI and just knowing that there are other constants is sufficient.

ConstantMeaningImplementation
M_E e #define M_E 2.71828182845904523536
M_LOG2E log2 e #define M_LOG2E 1.44269504088896340736
M_LOG10E log10 e #define M_LOG10E 0.434294481903251827651
M_LN2 ln 2 #define M_LN2 0.693147180559945309417
M_LN10 ln 10 #define M_LN10 2.30258509299404568402
M_PI π #define M_PI 3.14159265358979323846
M_PI_2 π/2 #define M_PI_2 1.57079632679489661923
M_PI_4 π/4 #define M_PI_4 0.785398163397448309616
M_1_PI 1/π #define M_1_PI 0.318309886183790671538
M_2_PI 2/π #define M_2_PI 0.636619772367581343076
M_2_SQRTPI 2 / √ π  #define M_2_SQRTPI 1.12837916709551257390
M_SQRT2  2  #define M_SQRT2 1.41421356237309504880
M_SQRT1_2 1 / √ 2  #define M_SQRT1_2 0.707106781186547524401
The complete math constant list.