You have probably studied functions in mathematics courses that you've taken: for example, the square root function and the trigonometric functions — sine, cosine, tangent, cotangent, secant, cosecant. You may have noticed that there's no square root character √ or keys labeled sin, cos, tan, cot, sec, and csc on your computer keyboard (unless you have a special one). So how do we compute these more complicated operations?

We'll look at how to write our own functions in later labs, but for now
we will look at some of the functions that C++ provides. Instead of building
them into the language, they are stored in
**function libraries** from which they can be loaded into
and used in programs when they are needed.

Using libraries to store function is done for at least three reasons:

- A library allows different programs to share the same function.
- If the functions were built into the language,
the binary executable version of
*every*program would be very large. - A library makes it easier to change the function.

Many of the libraries in C++ were originally C libraries. The names
of these libraries typically begin with the letter `c`

; for example,
`cmath`

or `cctype`

.

The `cmath`

library provides mathematical functions
such as sine, cosine, logarithm, exp, and so on and is generally known as
the math library. Some of the more common functions in this library are:

In each of these functions, the arguments

Function Description `fabs(x)`

Absolute value of `x`

`sin(x)`

Sine of `x`

(in radians)`cos(x)`

Cosine of `x`

(in radians)`tan(x)`

Tangent of `x`

(in radians)`asin(x)`

Inverse sine of `x`

(in radians)`acos(x)`

Inverse cosine of `x`

(in radians)`atan(x)`

Inverse tangent of `x`

(in radians)`sinh(x)`

Hyperbolic sine of `x`

(in radians)`cosh(x)`

Hyperbolic cosine of `x`

(in radians)`tanh(x)`

Hyperbolic tangent of `x`

(in radians)`log10(x)`

Base 10 logarithm of `x`

`log(x)`

Base e (natural) logarithm of `x`

`pow(x,y)`

`x`

raised to the power`y`

`exp(x)`

e raised to the power `x`

`sqrt(x)`

Square root of `x`

`ceil(x)`

Smallest integer not less than `x`

`floor(x)`

Largest integer not greater than `x`

`x`

and `y`

are
real numbers (i.e., `double`

s).
The declarations of these functions are stored in the system file
`cmath`

and so it is necessary to place the line

at the beginning of your program before you use any of these functions. (#include <cmath>

`c`

indicates that
this is a C library.)
The absolute value function,

is found in the C library

`abs(x)`

Absolute value of `x`

`cstdlib`

(along with some other functions such as a random number generator). To use
these functions, you may use
but this is usually not necessary because this library is commonly included automatically.#include <cstdlib>

(Note: For some compilers, you may also have to inform them where to find the definitions of these functions.)

Modify your program as follows. Comment out the code you have for dealing
with the integers `i`

and `j`

. Write code that declares
`x`

to be of type `double`

, inputs a real number
for `x`

, and then displayss the result
of applying each of the functions in the table in Question 3.8.1
to this value. Compile and execute your program.

Question #3.8.1: Use your program to fill in the following table with the values exactly as produced by your program:

`x`

function of `x`

result a. 1000.0 the base-10 log of `x`

b. 10.0 `x`

raised to the power 3.0c. 3.14159 sine of `x`

d. 3.14159 cosine of `x`

e. 9.9 the ceiling of `x`

f. 9.9 the floor of `x`

In addition to a library of mathematical functions, C++ also provides
a library `cctype`

containing a rich assortment of
character-processing functions, including the following:

In each of these functions, the argument

Function Returns `toupper(ch)`

the upper-case equivalent of `ch`

if`ch`

is lower case; for all other characters, it returns`ch`

`tolower(ch)`

the lower-case equivalent of `ch`

if`ch`

is uppercase; for all other characters, it returns`ch`

`isupper(ch)`

true if and only if `ch`

is one of 'A' through 'Z'`islower(ch)`

true if and only if `ch`

is one of 'a' through 'z'`isdigit(ch)`

true if and only if `ch`

is one of '0' through '9'`isalpha(ch)`

true if and only if `ch`

is one of 'A' through 'Z' or 'a' through 'z'`isspace(ch)`

true if and only if `ch`

is a white-space character (space, tab, newline, formfeed, carriage return, etc.)`iscntrl(ch)`

true if and only if `ch`

is not a printable character`ispunct(ch)`

true if and only if `ch`

is a punctuation character

`ch`

is of type `char`

.
You should become familiar with the available libraries, know basically what's in them, and (most importantly) know where you can find the details you need about them.

Question #3.8.2: Considering how the`cmath`

library is accessed in our program, how would you access the`cctype`

library?

Back to the Lab Exercise | Forward to the Next Experiment

Report errors to Larry Nyhoff (nyhl@cs.calvin.edu)