A complex number z = x + iy is an arrow from the origin to the point (x, y). Instead of its two coordinates you can give its polar form: the length r = |z| (abs(z)) and the angle \theta of the arrow, measured anticlockwise from the positive real axis, in radians. Then
z = r(\cos\theta + i\sin\theta)
The module cmath has cmath.phase(z), which returns the angle in the range (-\pi, \pi]: an arrow pointing straight down gets -\pi/2. The dial of a plotting tool shows directions from 0 up to (but not including) 2\pi instead, so straight down must read 3\pi/2.
Write to_polar(zs) that returns, for each number in zs in order, the pair (r, angle) with
anglein the range 0 \leangle< 2\pi,- the number
0written as(0.0, 0.0): it has no direction, and the dial shows angle 0 for it.
Examples
Input: zs = [1+1j, -1, -1j, 0]
Output: [(1.4142135623730951, 0.7853981633974483), (1.0, 3.141592653589793),
(1.0, 4.71238898038469), (0.0, 0.0)]
Explanation: 1+1j has length sqrt(2) and points at 45 degrees, pi/4.
-1 points left, pi. -1j points down: cmath.phase gives -pi/2, which is 3*pi/2 on the dial.
Input: zs = [-2-2j, 3, 0.5j]
Output: [(2.8284271247461903, 3.9269908169872414), (3.0, 0.0), (0.5, 1.5707963267948966)]
Explanation: -2-2j points down and to the left, at 225 degrees, 5*pi/4.
Constraints
- angles are in radians, not degrees
- answers are compared with a tolerance of
1e-6; do not round
Goals
- Write a complex number in polar form: a length r and an angle
- Move an angle from (-pi, pi] into [0, 2*pi)
- Decide the angle of 0 by a stated rule