TactivergeLite

Lite / Building Machines That Respond / Week 4

The math module: roots, angles and radians

A 2.4 m ramp rises at 30°. Python says its height is −2.37 m. What did the program forget?

About 35 min · 3 goals · Checked by automated tests · Full interactive version

Why should I care?

Ramps, braces, robot arms and wheels all need square roots and angles. Python's math module has them, but with one famous trap: its trigonometry works in radians. Mixing up degrees and radians has caused real engineering errors.

Predict

What does math.sin(30) give in Python?

  1. 0.5
  2. About −0.988
  3. An error: 30 is not an angle
Guess first, then reveal

About −0.988. math.sin treats 30 as 30 radians (about 1719°). For 30°, use math.sin(math.radians(30)), which gives 0.49999999999999994, i.e. 0.5 to within float precision.

Discover

Choose 'Ramp height (math)'. Compare the version with math.radians to the one without, at several angles: is the wrong one ever right? Then choose 'Diagonal brace (math)' and find out why √w + √h is not the brace length.

What the interactive shows (text version)

The precedence explorer's ramp preset compares length * math.sin(math.radians(angle)) with length * math.sin(angle). For 2.4 m at 30° the first gives 1.1999999999999997 m (matches 1.2 m) and the second gives −2.3712758978228683 m (wrong). The brace preset shows math.sqrt(0.3 ** 2 + 0.4 ** 2) = 0.5 m, while math.sqrt(w ** 2) + math.sqrt(h ** 2) gives 0.7 m.

You can also open the interactive on the full site when you have a faster connection.

Understand

Import, then call

import math
length_m = 2.4
angle_deg = 30
height_m = length_m * math.sin(math.radians(angle_deg))
print(round(height_m, 3))   # 1.2
w_m, h_m = 0.3, 0.4
brace_m = math.sqrt(w_m ** 2 + h_m ** 2)
print(brace_m)              # 0.5

import math once at the top, then use math. before each function.

Radians in, radians out

math.sin, math.cos and math.tan expect radians, and math.atan2 returns radians. Convert at the boundary: math.radians(deg) going in, math.degrees(rad) coming out. Name variables _deg or _rad so the unit is never in doubt.

Sanity checks for geometry

A ramp's height must be less than its length. A diagonal must be longer than either side but shorter than both added together. sin and cos are always between −1 and 1. A negative height or a brace shorter than a side means a unit or bracket error.

Explain it another way

Intuitive

Degrees and radians are like inches and centimetres for angles. The math module only speaks radians, so you translate first.

Technical

math functions are implemented by the platform's C maths library on IEEE doubles. math.radians(x) returns x·π/180; results carry tiny rounding errors, so sin(π/6) prints as 0.49999999999999994.

Mathematical

Opposite = hypotenuse × sin θ; adjacent = hypotenuse × cos θ; hypotenuse = √(a² + b²). For a 0.3 m × 0.4 m frame the brace is √(0.09 + 0.16) = √0.25 = 0.5 m.

Applied

A robot arm's tip position is x = L·cos θ, y = L·sin θ for each link. Feeding encoder angles in degrees to cos and sin would put the gripper somewhere completely different.

Derivation: Degrees to radians
  1. A full turn is 360°. Measured as arc length on a circle of radius 1, a full turn is the circumference 2π, so 360° = 2π rad.

  2. Divide both sides by 360: 1° = 2π/360 = π/180 rad.

  3. So θ_rad = θ_deg × π / 180. For 30°: 30 × π / 180 = π/6 ≈ 0.5236 rad.

  4. math.radians(30) does exactly this calculation. Then 2.4 × sin(π/6) = 2.4 × 0.5 = 1.2 m.

Worked example 1

A wheelchair ramp may rise at most 1 in 12. What angle is that, and how long a ramp reaches a 0.5 m step?

Slope = 1/12, so θ = math.degrees(math.atan(1 / 12)) ≈ 4.76°. Horizontal run = 0.5 × 12 = 6.0 m. Ramp length = math.sqrt(6.0 2 + 0.5 2) ≈ 6.02 m.

Common mix-ups

Practice

Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.

m

Practice 2. Which line correctly computes cos of 60°?

cm

Practice 4. Why is math.sqrt(w ** 2) + math.sqrt(h ** 2) not the brace length?

rad

Apply it

Write a program that prints the tip position (x, y) of a 0.25 m robot arm link for angles 0°, 30°, 60° and 90°. Check that x² + y² is always 0.25² (to float precision), then compare with the kinematics lab.

Reflect

How will you make sure, in every program you write, that an angle is in the unit the function expects?

Write your answer on paper or in your own notes. Lite never stores what you write.

Go further

← Rearrange the formula, then compute: an LED resistorDecisions from measurements: if, elif, else →