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.
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.
Value | Meaning | Example |
---|---|---|
#define M_PI 3.14159265358979323846 | π = M_PI | a = M_PI * r * r; |
The cmath header provides many other non-standard math constants, and each one begins with M_, signifying its specification in the math header file.
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); |
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.
Function Declaration | Meaning | Example |
---|---|---|
double sqrt (double x); | √ x = sqrt(x) | hypotenuse = sqrt(pow(a,2) + pow(b,2)); |
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++ 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.
Constant | Meaning | Implementation |
---|---|---|
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 |