Skip to main content

Section 4.11 Trigonometric Functions

One aspect of the cmath library that can cause confusion is the units used for trigonometric functions β€” sin, cos, and tan. These functions use radians to measure angles, not degrees.
You may be more most familiar with measuring angles in degrees. But there is another way to measure angles: radians. Just as we can measure distance in inches or in centimeters, we can measure angles in degrees or radians. Radians are the preferred unit for advanced mathematics and for computation. When calculating angles to solve math problems, draw computer graphics, or do other tasks with C++, we need to convert any degree measurements to radians before working with them
In degrees, a full revolution is defined as 360. Radian units are based on \(\pi\) (pi). In radians, a full revolution is defined as \(2\pi\) radians. Because pi is approximately 3.14159, an approximate way of writing \(2\pi\) radians is 6.28318 radians. In math, we generally write values in terms of \(\pi\) like \(2\pi\) or \(\frac{\pi}{2}\text{.}\) But in code, those values will be expressed as the decimals ~6.28 or ~1.57.
A full circle is 360 degrees or 2 pi radians. A quarter circle is 90 degrees or 0.5 pi radians.
Figure 4.11.1. Degrees and angles are just different ways of measuring angles.
Because \(360\text{ degrees } = 2\pi \text{ radians}\) we can use the ratio \(\frac{2\pi \text{ radians}}{360\text{ degrees}}\) to convert between them. That ratio simplifies to \(\frac{\pi \text{ radians}}{180\text{ degrees}}\text{.}\) Thus:
  • To convert from degrees to radians, you divide by 180 and multiply by \(\pi\text{.}\) For example, to convert 90 degrees, we do:
    \begin{equation*} 90 \cancel{\text{ degrees }} \cdot \frac{\pi \text{ radians}}{180\cancel{\text{ degrees}}} = \frac{\pi}{2} \text{ radians} = ~1.5708 \text{ radians} \end{equation*}
  • To convert from radians to degrees, you do the opposite: divide by \(\pi\) and multiply by 180. For example, to convert 1.2 radians, we do:
    \begin{equation*} 1.2 \cancel{\text{ radians}} \cdot \frac{180\text{ degrees}}{\pi \cancel{\text{ radians}}} = 68.7549 \text{ degrees} \end{equation*}
In C++, to get the value of \(\pi\text{,}\) you can write std::numbers::pi (or if you have used using namespace std; just numbers::pi).
Listing 4.11.2.
Notice that when we call the sin function, we give it the angle in radians, not the value in degrees. The correct value for the sine of 90 degrees is 1. But if you pass 90 to sin you will not get that answer because the sin function will interpret it as 90 radians.

Note 4.11.1.

numbers::pi is a new feature as of C++20. For older compilers, you need to define your own constant for pi, or calculate a value for it using an expression that should result in the value \(1\pi\) like pi = acos(-1).
If you use the inverse trigonometric functions (asin, acos, atan) to calculate an angle’s size, the answer will be returned in radians. So, to get the measure in degrees, you need to multiply by 180 and then divide by \(\pi\text{:}\)
Listing 4.11.3.

Insight 4.11.2.

If you are given degrees to work with in C++, start by converting them to radians. Then work with that value in your calculations. Don’t convert back to degrees until you are done with your math. It can help to name your variables using the units of the values you are storing (like angleADegrees).

Checkpoint 4.11.1.

You have attempted of activities on this page.