Node:Mathematical functions, Next:Questions for Chapter 13, Previous:Common library functions, Up:Libraries

### Mathematical functions

Let us now examine some simple math library functions. (This section presupposes some familiarity on your part with trigonometry. If you have none, you might want to skip this section for now - but reading it won't hurt you!)

The following mathematical functions, among others, are available on GNU
systems. Many of the functions described below are macros, so the usual
caveats about macro parameters apply. (See Macro functions.) All of
these functions require parameters of type `double`

or ```
long
float
```

. Constants used must be written in floating point form: for
instance, write `7.0`

instead of just `7`

.

Here is a list of some functions you can expect to find in the headers
`math.h`

, `tgmath.h`

, and `limits.h`

.

`abs`

- Returns the unsigned value of the parameter in brackets. This function
is a macro; see
`fabs`

for a proper function version. `acos`

- Returns the arccosine (or inverse cosine) of the parameter, which must lie
between -1.0 and +1.0 inclusive.
(The result is always in radians.)
`asin`

- Returns the arcsine (or inverse sine) of the parameter, which must lie
between -1.0 and +1.0 inclusive.
(The result is always in radians.)
`atan`

- Returns the arctangent (or inverse tangent) of the parameter.
(The result is always in radians.)
`atan2`

- This is a special function for calculating the inverse tangent of the
second parameter divided by the first.
`atan2`

will find the result more accurately than`atan`

will.result = atan2 (x, y); result = atan2 (x, 3.14);

`ceil`

- Returns the ceiling for the parameter: that is, the integer
just above it. In effect, rounds the parameter up.
`cos`

- Returns the cosine of the parameter in radians.
(The parameter is also assumed to be specified in radians.)
`cosh`

- Returns the hyperbolic cosine of the parameter.
`exp`

- Returns the exponential function of the parameter (i.e.
*e*to the power of the parameter). `fabs`

- Returns the absolute or unsigned value of the parameter in brackets.
This is the version that is a proper function; see
`abs`

if you want one that is a macro. `floor`

- Returns the floor for the parameter: that is, the integer just below it.
In effect, rounds the parameter down to the nearest integer value,
i.e. truncates it.
`log`

- Returns the natural logarithm of the parameter. The parameter used must
be greater than zero, but does not have to be declared as unsigned.
`log10`

- Returns the base 10 logarithm of the parameter. The parameter used must
be greater than zero, but does not have to be declared as unsigned.
`pow`

- Returns the first parameter raised to the power of the second.
result = pow (x,y); /*raise x to the power y */ result = pow (x,2); /* square x */

`sin`

- Returns the sine of the parameter in radians.
(The parameter is also assumed to be specified in radians.)
`sinh`

- Returns the hyperbolic sine of the parameter. (Pronounced "shine" or
"sinch".)
`sqrt`

- Returns the positive square root of the parameter.
`tan`

- Returns the tangent of the parameter in radians.
(The parameter is also assumed to be specified in radians.)
`tanh`

- Returns the hyperbolic tangent of the parameter.

Here is a code example that uses a few of the math library routines listed above.

#include <stdio.h> #include <math.h> int main() { double my_pi; my_pi = 4 * atan(1.0); /* Print the value of pi we just calculated, to 32 digits. */ printf ("my_pi = %.32f\n", my_pi); /* Print value of pi from math library, to 32 digits. */ printf ("M_PI = %.32f\n", M_PI); return 0; }

If you save the above example as `pi.c`

, you will have to
enter a command such as the one below to compile it.

gcc pi.c -o pi -lm

When you compile and run the code example, it should print the following
results:

my_pi = 3.14159265358979311599796346854419 M_PI = 3.14159265358979311599796346854419