This chapter contains information about functions for performing mathematical computations, such as trigonometric functions. Most of these functions have prototypes declared in the header file math.h. The complex-valued functions are defined in complex.h. All mathematical functions which take a floating-point argument have three variants, one each for double, float, and long double arguments. The double versions are mostly defined in ISO C89. The float and long double versions are from the numeric extensions to C included in ISO C99.
Which of the three versions of a function should be used depends on the situation. For most calculations, the float functions are the fastest. On the other hand, the long double functions have the highest precision. double is somewhere in between. It is usually wise to pick the narrowest type that can accommodate your data. Not all machines have a distinct long double type; it may be the same as double.
The header math.h defines several useful mathematical constants. All values are defined as preprocessor macros starting with M_. The values provided are:
The base of natural logarithms.
The logarithm to base 2 of M_E.
The logarithm to base 10 of M_E.
The natural logarithm of 2.
The natural logarithm of 10.
Pi, the ratio of a circle's circumference to its diameter.
Pi divided by two.
Pi divided by four.
The reciprocal of pi (1/pi)
Two times the reciprocal of pi.
Two times the reciprocal of the square root of pi.
The square root of two.
The reciprocal of the square root of two (also the square root of 1/2).
These constants come from the Unix98 standard and were also available in 4.4BSD; therefore they are only defined if _BSD_SOURCE or _XOPEN_SOURCE=500, or a more general feature select macro, is defined. The default set of features includes these constants. the section called “Feature Test Macros”.
All values are of type double. As an extension, the GNU C library also defines these constants with type long double. The long double macros have a lowercase l appended to their names: M_El, M_PIl, and so forth. These are only available if _GNU_SOURCE is defined.
Note: Some programs use a constant named PI which has the same value as M_PI. This constant is not standard; it may have appeared in some old ATT headers, and is mentioned in Stroustrup's book on C++. It infringes on the user's name space, so the GNU C library does not define it. Fixing programs written to expect it is simple: replace PI with M_PI throughout, or put -DPI=M_PI on the compiler command line.