# Mathematical Functions in C++

A function is a group of statements that performs a specific task. You have already used the pow(a, b) function to compute ab in Section 2.8.4, “Exponent Operations,” and the rand() function to generate a random number in Section 3.9, “Generating Random Numbers.” This section introduces other useful functions. They can be categorized as trigonometric functions, exponent functions, and service functions. Service functions include the rounding, min, max, and absolute functions.

### 1. Trigonometric Functions

C++ provides the following functions as shown in Table 4.1 for performing trigonometric functions in the cmath header:

The parameter for sin, cos, and tan is an angle in radians. The return value for asin, acos, and atan is an angle in radians in the range between -π/2 and π/2. One degree is equal to π/180 in radians, 90 degrees is equal to π/2 in radians, and 30 degrees is equal to π/6 in radians.

Assume PI is a constant with value 3.14159. Here are some examples of using these functions:

sin(0) returns 0.0

sin(270 * PI / 180) returns -1.0

sin(PI / 6) returns 0.5

sin(PI / 2) returns 1.0

cos(0) returns 1.0

cos(PI / 6) returns 0.866

cos(PI / 2) returns 0

asin(0.5) returns 0.523 599 (same as π/6)

acos(0.5) returns 1.0472 (same as π/3)

atan(1.0) returns 0.785398 (same as π/4)

### 2. Exponent Functions

There are five functions related to exponents in the cmath header as shown in Table 4.2.

Assume E is a constant with value 2.71828. Here are some examples of using these functions:

exp(1.0) returns 2.71828

log(E) returns 1.0

log10(10.0) returns 1.0

pow(2.0, 3) returns 8.0

sqrt(4.0) returns 2.0

sqrt(10.5) returns 3.24

### 3. Rounding Functions

The cmath header contains the functions for obtaining rounding as shown in Table 4.3.

For example,

ceil(2.1) returns 3.0

ceil(2.0) returns 2.0

ceil(-2.0) returns -2.0

ceil(-2.1) returns -2.0

floor(2.1) returns 2.0

floor(2.0) returns 2.0

floor(-2.0) returns -2.0

floor(-2.1) returns -3.0

### 4. The min, max, and abs Functions

The min and max functions return the minimum and maximum numbers of two numbers (int, long, float, or double). For example, max(4.4, 5.0) returns 5.0, and min(3, 2) returns 2.

The abs function returns the absolute value of the number (int, long, float, or double). For example,

max(2, 3) returns 3

max(2.5, 3.0) returns 3.0

min(2.5, 4.6) returns 2.5

abs(-2) returns 2

abs(-2.1) returns 2.1

### 5. Case Study: Computing Angles of a Triangle

You can use the math functions to solve many computational problems. Given the three sides of a triangle, for example, you can compute the angles by using the following formula:

A = acos((a * a – b * b – c * c) / (-2 * b * c))

B = acos((b * b – a * a – c * c) / (-2 * a * c))

C = acos((c * c – b * b – a * a) / (-2 * a * b))

Don’t be intimidated by the mathematic formula. As we discussed in Listing 2.11, ComputeLoan.cpp, you don’t have to know how the mathematical formula is derived in order to write a program for computing the loan payments. In this example, given the length of three sides, you can use this formula to write a program to compute the angles without knowing how the formula is derived. In order to compute the lengths of the sides, we need to know the coordinates of three corner points and compute the distances between the points.

Listing 4.1 is an example of a program that prompts the user to enter the x- and y-coordinates of the three corner points in a triangle and then displays the triangle’s angles.

Listing 4.1 ComputeAngles.cpp

1 #include <iostream>

2 #include <cmath>

3 using namespace std;

4

5 int main()

6 {

7     // Prompt the user to enter three points

8     cout << “Enter three points: “;

9     double x1, y1, x2, y2, x3, y3;

10    cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;

11

12    // Compute three sides

13    double a = sqrt((x2 – x3) * (x2 – x3) + (y2 – y3) * (y2 – y3));

14    double b = sqrt((x1 – x3) * (x1 – x3) + (y1 – y3) * (y1 – y3));

15    double c = sqrt((x1 – x2) * (x1 – x2) + (y1 – y2) * (y1 – y2));

16

17    // Obtain three angles in radians

18    double A = acos((a * a – b * b – c * c) / (-2 * b * c));

19    double B = acos((b * b – a * a – c * c) / (-2 * a * c));

20    double C = acos((c * c – b * b – a * a) / (-2 * a * b));

21

22    // Display the angles in degress

23    const double PI = 3.14159;

24    cout << “The three angles are ” << A * 180 / PI << ” “

25    << B * 180 / PI << ” ” << C * 180 / PI << endl;

26

27    return 0;

28 }

The program prompts the user to enter three points (line 10). This prompting message is not clear. You should give the user explicit instructions on how to enter these points as follows:

cout << “Enter the coordinates of three points separated “

<< “by spaces like x1 y1 x2 y2 x3 y3: “;

Note that the distance between two points (x1, y1) and (x2, y2) can be computed using the formula . The program applies this formula to compute the three sides (lines 13-15), and then applies the formula to compute the angles in radians (lines 18-20). The angles are displayed in degrees (lines 24-25). Note that 1 radian is 180/π degrees.

Source: Liang Y. Daniel (2013), Introduction to programming with C++, Pearson; 3rd edition.