Problem 188252 · easy · Robotics · Level 01 Poses and Frames

The Gyro That Never Forgets

radians · angle wrapping · headings · loops · modular arithmetic

A floor-cleaning robot measures its heading with a gyroscope that adds up every turn it ever made. After a few spins on the spot the gyro happily reports headings such as 7.0 or -13.2 radians. Directions repeat every full turn: adding or subtracting 2\pi radians (one turn) points the robot the same way.

Headings here are in radians, measured counter-clockwise from the world's +x axis. The robot's software wants every heading in the range

-\pi < \theta \le \pi

called wrapping the angle. Each direction has exactly one heading in that range. Note the ends: \pi (pointing along -x) stays \pi, and -\pi, the same direction, is written as +\pi.

Write wrap_headings(headings) that returns the list of wrapped headings, in the same order.

Examples

Input:  headings = [7.0, -4.0, 3.141592653589793, -3.141592653589793, 0.5]
Output: [0.7168146928204138, 2.2831853071795862, 3.141592653589793, 3.141592653589793, 0.5]
Explanation: 7.0 - 2*pi = 0.7168; -4.0 + 2*pi = 2.2832; pi stays; -pi is written as +pi;
0.5 is already in range.

Input:  headings = [0, 12.566370614359172, -100.0]
Output: [0.0, 0.0, 0.5309649148733797]
Explanation: 12.566... is exactly two full turns, so the robot faces +x again.
-100 + 16*pi = 0.531.

Constraints

  • answers are compared with a tolerance of 1e-6; do not round

Goals

  • See that adding or subtracting $2\pi$ leaves a direction unchanged
  • Bring any angle into the range $(-\pi, \pi]$
  • Settle the boundary: $-\pi$ becomes $+\pi$
Starting Python…