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:
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 xsin(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 xlog(x)Base e (natural) logarithm of xpow(x,y)xraised to the poweryexp(x)e raised to the power xsqrt(x)Square root of xceil(x)Smallest integer not less than xfloor(x)Largest integer not greater than x
x and y are
real numbers (i.e., doubles).
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. (Note: The initial letter#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:
xfunction of xresult a. 1000.0 the base-10 log of xb. 10.0 xraised to the power 3.0c. 3.14159 sine of xd. 3.14159 cosine of xe. 9.9 the ceiling of xf. 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 chifchis lower case; for all other characters, it returnschtolower(ch)the lower-case equivalent of chifchis uppercase; for all other characters, it returnschisupper(ch)true if and only if chis one of 'A' through 'Z'islower(ch)true if and only if chis one of 'a' through 'z'isdigit(ch)true if and only if chis one of '0' through '9'isalpha(ch)true if and only if chis one of 'A' through 'Z' or 'a' through 'z'isspace(ch)true if and only if chis a white-space character (space, tab, newline, formfeed, carriage return, etc.)iscntrl(ch)true if and only if chis not a printable characterispunct(ch)true if and only if chis 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 thecmathlibrary is accessed in our program, how would you access thecctypelibrary?