OBJECTIVES In this chapter we develop problem solutions containing:
2.7 Basic Functions Included in the C++ Standard Library
Arithmetic expressions that solve engineering problems often require computations other than addition, subtraction, multiplication, and division. For example, many expressions require the use of logarithms, exponentials, and trigonometric functions. In this section, we discuss pre-defined mathematical functions and character functions that are available in the Standard C++
library. For example, the following statement computes the sine of an angle theta and stores the result in the object b:
b = sin(theta);
The sin() function assumes that the argument theta is in radians. If theta contains a value in degrees, we can convert the degrees to radians with a separate statement. (Recall that 180 degrees= π radians.)
Section 2.7 Basic Functions Included in the C++ Standard Library 77 const double PI = acos(-1.0);
...
theta_rad = theta*PI/180.0;
b = sin(theta_rad);
The conversion can also be specified within the function reference b = sin(theta*PI/180.0);
A function reference, such as sin(theta), represents a single value. The parenthe-ses following the function name contain the inputs to the function, which are called function function arguments
arguments. A function may contain no arguments, one argument, or many arguments, de-pending on its definition. If a function contains more than one argument, it is very important to list the arguments in the correct order. Some functions also require that the arguments be in specific units. For example, the trigonometric functions assume that arguments are in radi-ans. Most of the mathematical functions assume that the arguments are double values; if a different type argument is used, it is promoted to a double before the function is executed.
A function reference can also be part of the argument of another function reference. For example, the following statement computes the logarithm of the absolute value of x:
b = log(abs(x));
When one function is used to compute the argument of another function, be sure to enclose the argument of each function in its own set of parentheses. This nesting of functions is also called composition of functions.
composition of
functions We now discuss several categories of functions that are commonly used in engineering computations. Other functions will be presented throughout the remaining chapters as we dis-cuss relevant subjects. Appendix A also contains more information on the functions included in the Standard C++ library.
Elementary Math Functions
The elementary math functions include functions to perform a number of common computa-tions such as computing the absolute value of a number and the square root of a number. In addition, they also include a group of functions used to perform rounding. These functions as-sume that the type of all arguments is double, and the functions all return a double; if an argument is not a double, a conversion will occur using the rules described in Section 2.2.
The following preprocessor directive should be used in programs referencing the mathemati-cal functions available in the Standard C++ library:
#include <cmath>
We now list these functions with a brief description:
fabs(x) This function computes the absolute value of x.
sqrt(x) This function computes the square root of x, where x>= 0.
pow(x,y) This function is used for exponentiation,
and computes the value of x to the y power, or xy.
Errors occur if x= 0 and y <= 0, or if x < 0 and y is not an integer.
ceil(x) This function rounds x to the nearest integer toward infinity.
For example, ceil(2.01) is equal to 3.
floor(x) This function rounds x to the nearest integer toward negative infinity.
For example, floor(2.01) is equal to 2.
78 Chapter 2 Simple C++ Programs
exp(x) This function computes the value of ex,
where e is the base for natural logarithms, or approximately 2.718282.
log(x) This function returns ln x, the natural logarithm of x to the base e.
Errors occur if x <= 0.
log10(x) This function returns log10x, the common logarithm of x to the base 10.
Errors occur if x <= 0.
Remember that the logarithm of a negative value or zero does not exist, and thus an execution error occurs if you use a logarithm function with a negative value for its argument.
Practice!
Evaluate the following expressions:
1. floor(-2.6) 2. ceil(-2.6)
3. pow(2.0,-3) 4. sqrt(floor(10.7))
5. abs(-10*2.5) 6. floor(ceil(10.8))
7. log10(100) + log10(0.001) 8. abs(pow(-2,5.0))
Trigonometric Functions
The trigonometric functions, also included in file cmath, assume that all arguments are of trigonometric
functions type double and that each function returns a value of double. In addition, as previously stated, the trigonometric functions also assume that angles are represented in radians. To con-vert radians to degrees, or degrees to radians, use the following conversions:
const double PI = acos(-1.0);
...
angle_deg = angle_rad*(180/PI);
angle_rad = angle_deg*(PI/180);
sin(x) This function computes the sine of x, where x is in radians.
cos(x) This function computes the cosine of x, where x is in radians.
tan(x) This function computes the tangent of x, where x is in radians.
asin(x) This function computes the arcsine, or inverse sine, of x, where x must be in the range [−1, 1].
The function returns an angle in radians in the range [−π/2, π/2].
acos(x) This function computes the arccosine, or inverse cosine, of x, where x must be in the range [−1, 1].
The function returns an angle in radians in the range [0, π].
atan(x) This function computes the arctangent, or inverse tangent, of x.
The function returns an angle in radians in the range [−π/2, π/2].
atan2(y,x) This function computes the arctangent or inverse tangent of the value y/x.
The function returns an angle in radians in the range [−π, π].
Note that the atan function always returns an angle in Quadrant I or IV, whereas the atan2 function returns an angle that can be in any quadrant, depending on the signs of x and y. Thus, in many applications, the atan2 function is preferred over the atan function.
Section 2.7 Basic Functions Included in the C++ Standard Library 79 The other trigonometric and inverse trigonometric functions can be computed using the following equations [10]:
Using degrees instead of radians is a common error in programs with trigonometric functions.
Practice!
In problems 1 through 3, give assignment statements for computing the indicated values, assuming that the objects have been declared and given appropriate values. Also assume that the following declarations have been made:
const double g = 9.8:
const double PI = acos(-1.0);
1. Velocity computation:
3. Distance of the center of gravity from a reference plane in a sector of a hollow cylinder:
Center= 38.1972(r3− s3) sin a (r2− s2)a
In problems 4 through 6, give the equations that correspond to the assignment statement.
4. Electrical oscillation frequency:
frequency = 1/sqrt(2*pi*c/L);
5. Range for a projectile:
range = (v0*v0/g)*sin(2*theta);
6. Speed of a disk at the bottom of an incline:
v = sqrt(2*g*h/(1 + I/(m*pow(r,2))));
80 Chapter 2 Simple C++ Programs
Hyperbolic Functions*
Hyperbolic functions are functions of the natural exponential function ex; the inverse hy-Hyperbolic
functions perbolic functions are functions of the natural logarithm function ln x. These functions are useful in specialized applications such as the design of some types of digital filters. The Stan-dard C++ library includes several hyperbolic functions that are described next. The hyperbolic functions are included in the Standard C++ library, and a preprocessor directive including the information in cmath should be used with these functions.
cmath
sinh(x) This function computes the hyperbolic sine of x, which is equal to ex+ e−x
2 .
cosh(x) This function computes the hyperbolic cosine of x, which is equal to ex− e−x
2 .
tanh(x) This function computes the hyperbolic tangent of x, which is equal to sinh x
cosh x.
Additional hyperbolic functions and the inverse hyperbolic functions can be computed using these relationships [10]:
Many of the hyperbolic functions and inverse trigonometric functions have restrictions on the range of acceptable values for arguments.If the arguments are entered from the key-board, remind the user of the range restrictions.In the next chapter, we introduce C++ state-ments that allow you to determine whether a value is in the proper range during program execution.
Section 2.8 Problem Solving Applied: Velocity Computation 81
Practice!
Give assignment statements for calculating the following values, given the value of x (assume that the value of x is in the proper range of values for the calculations).
1. coth x 2. sech 3 x
3. csch 4 x 4. acots 6 x
5. acosh x 6. acsch x
Character Functions
The Standard C++ library contains many predefined functions for use with character data.
These functions fall into two categories: one set of functions is used to convert characters between uppercase and lowercase, and the other set is used to perform character comparisons.
The following preprocessor directive should be used in programs referencing these character functions:
#include <cctype>
The following statement converts the lowercase letter stored in the object ch to an uppercase character and stores the result in the character object ch_upper:
ch_upper = toupper(ch);
If ch is a lowercase letter, the function toupper() returns the corresponding uppercase letter; otherwise, the function returns ch. Note that no change is made to the argument ch.
The character comparison functions return a nonzero value if the comparison is true;
otherwise they return zero. The following statement calls the function isdigit(). The function isdigit() will return a nonzero result if the value of ch is a digit (0–9) or the value 0 (false) if ch is not a digit:
digit = isdigit(ch);
A list of these functions along with a brief explanation is given in Appendix A.