Course 6.1Lesson 1 of 3
Python measures angles in radians, so degrees must be converted with math.radians. The unit circle shows that the point at an angle is (cos, sin), which fixes the signs in every quadrant. A loop builds the table of the six ratios, and a tolerance, not ==, compares the answers, since sin(pi) is not exactly zero.
Try the code and the simulations beside the lesson.
You have measured angles in degrees since Class 6. A full turn is , a half turn is and a right angle is . Mathematicians and computers prefer another unit, the radian. A full turn is radians, so a half turn is exactly radians.
| Degrees | Radians | Decimal |
|---|---|---|
| 0 | 0 | 0 |
| 30 | π/6 | 0.5236 |
| 45 | π/4 | 0.7854 |
| 60 | π/3 | 1.0472 |
| 90 | π/2 | 1.5708 |
| 180 | π | 3.1416 |
| 270 | 3π/2 | 4.7124 |
In Python, math.radians(180) gives 3.141592653589793, and math.degrees(math.pi / 2) gives 90.0. The number is stored as math.pi. Never type 3.14: it has only three digits, and the error shows up in the third decimal of every answer.
Every trigonometric function in Python expects radians. If you give it degrees, it does not complain. It just reads your number as radians and gives a wrong answer.
math.sin(30) is about , because radians is about five turns, not .math.sin(math.radians(30)) is the sine of , about .The numpy versions work on whole arrays: np.sin, np.cos, np.tan, np.radians and np.degrees.
In the chapter Introduction to Trigonometry you found sine and cosine from a right triangle: . Now draw a circle of radius with its centre at the origin. Turn a radius anticlockwise from the positive x-axis by an angle . Its tip is a point . Drop a perpendicular from to the x-axis.
The triangle has a hypotenuse of . So the side along the axis is , and the upright side is . That gives the whole definition:
This works for every angle, also for angles above , negative angles and angles above a full turn.
Read the signs from the picture. In each quadrant, ask on which side of the axes the point lies.
The tangent is . It is undefined where , at and .
An angle bigger than a turn lands on the same point as a smaller one. The angle is the same as . In Python, 380 % 360 gives 20, and -60 % 360 gives 300, because % never returns a negative number for a positive divisor.
Besides sine, cosine and tangent, there are three reciprocal ratios: , and . Python has no math.sec, so you write 1 / math.cos(a).
A loop builds a table of the ratios of the standard angles:
The standard values you learnt in Class 10 appear: , and . Where a ratio is undefined, as is, a guard such as if abs(c) > 1e-9: keeps the program from dividing by a number that is nearly zero.
Run print(math.sin(math.pi)). It prints 1.2246467991473532e-16, not 0.0. The number cannot be stored exactly. The computer keeps about digits, so the answer carries a rounding error of about . The same happens in math.sin(math.radians(30)), which gives 0.49999999999999994, not 0.5.
So a test such as math.sin(math.radians(30)) == 0.5 is False. Compare with a tolerance instead:
math.isclose(a, b) is true when and agree to about nine digits;math.isclose(x, 0, abs_tol=1e-9), because a relative tolerance means nothing next to zero;np.isclose and np.allclose.The program converts angles, prints a table of ratios in a loop, shows the rounding error in , and draws the unit circle with the point at .
math.sin(30) is not the sine of . Convert with math.radians first.3.14 for . Use math.pi or np.pi.==. Use math.isclose or np.allclose.sin without math. Python then says name 'sin' is not defined. Write math.sin, or import it with from math import sin.math.radians.sin, cos and tan take radians.math.isclose(a, b, abs_tol=...), never with ==.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
math.cos(math.radians(120)) give, and in which quadrant does the point lie?math.sin(90) and gets 0.894, not 1.0. What went wrong?-30 % 360 give, and which angle on the circle is it?math.sin(math.pi) == 0 print False, and what should you write?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.