next up previous
Next: Data input and output Up: Scientific programming in C Previous: Operators

Library functions

The C language is accompanied by a number of standard library functions which carry out various useful tasks. In particular, all input and output operations (e.g., writing to the terminal) and all math operations (e.g., evaluation of sines and cosines) are implemented by library functions.

In order to use a library function, it is necessary to call the appropriate header file at the beginning of the program. The header file informs the program of the name, type, and number and type of arguments, of all of the functions contained in the library in question. A header file is called via the preprocessor statement

#include   <filename>
where filename represents the name of the header file.

A library function is accessed by simply writing the function name, followed by a list of arguments, which represent the information being passed to the function. The arguments must be enclosed in parentheses, and separated by commas: they can be constants, variables, or more complex expressions. Note that the parentheses must be present even when there are no arguments.

The C math library has the header file math.h, and contains the following useful functions:

Function        Type        Purpose
--------        ----        -------

acos(d)         double      Return arc cosine of d (in range 0 to pi)
asin(d)         double      Return arc sine of d (in range -pi/2 to pi/2)
atan(d)         double      Return arc tangent of d (in range -pi/2 to pi/2)
atan2(d1, d2)   double      Return arc tangent of d1/d2 (in range -pi to pi)
cbrt(d)         double      Return cube root of d
cos(d)          double      Return cosine of d
cosh(d)         double      Return hyperbolic cosine of d
exp(d)          double      Return exponential of d
fabs(d)         double      Return absolute value of d
hypot(d1, d2)   double      Return sqrt(d1 * d1 + d2 * d2)
log(d)          double      Return natural logarithm of d
log10(d)        double      Return logarithm (base 10) of d
pow(d1, d2)     double      Return d1 raised to the power d2
sin(d)          double      Return sine of d
sinh(d)         double      Return hyperbolic sine of d
sqrt(d)         double      Return square root of d
tan(d)          double      Return tangent of d
tanh(d)         double      Return hyperbolic tangent of d
Here, Type refers to the data type of the quantity that is returned by the function. Moreover, d, d1, etc. indicate arguments of type double.

A program that makes use of the C math library would contain the statement

#include   <math.h>
close to its start. In the body of the program, a statement like
x = cos(y);
would cause the variable x to be assigned a value which is the cosine of the value of the variable y (both x and y should be of type double).

Note that math library functions tend to be extremely expensive in terms of CPU time, and should, therefore, only be employed when absolutely necessary. The classic illustration of this point is the use of the pow() function. This function assumes that, in general, it will be called with a fractional power, and, therefore, implements a full-blown (and very expensive) series expansion. Clearly, it is not computationally efficient to use this function to square or cube a quantity. In other words, if a quantity needs to be raised to a small, positive integer power then this should be implemented directly, instead of using the pow() function: i.e., we should write x * x rather than pow(x, 2), and x * x * x rather than pow(x, 3), etc. (Of course, a properly designed exponentiation function would realize that it is more efficient to evaluate small positive integer powers by the direct method. Unfortunately, the pow() function was written by computer scientists!)

The C math library comes with a useful set of predefined mathematical constants:

Name        Description    
----        -----------    

M_PI        Pi, the ratio of a circle's circumference to its diameter. 
M_PI_2      Pi divided by two.
M_PI_4      Pi divided by four.    
M_1_PI      The reciprocal of pi (1/pi).
M_SQRT2     The square root of two.
M_SQRT1_2   The reciprocal of the square root of two 
             (also the square root of 1/2). 
M_E         The base of natural logarithms.

The other library functions commonly used in C programs will be introduced, as appropriate, during the remainder of this discussion.

next up previous
Next: Data input and output Up: Scientific programming in C Previous: Operators
Richard Fitzpatrick 2006-03-29