Skip to content

Commit aa9bace

Browse files
author
Julia
committed
new function to convert polar number to rectangular
1 parent 03a4251 commit aa9bace

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import math
2+
3+
4+
def pol_to_rec(mod: float, mag: float):
5+
### https://en.wikipedia.org/wiki/Polar_coordinate_system
6+
"""
7+
>>> pol_to_rec(5, 53)
8+
(3.01, 3.99)
9+
>>> pol_to_rec(10, 0)
10+
(10.0, 0.0)
11+
>>> pol_to_rec(0, 45)
12+
(0.0, 0.0)
13+
>>> pol_to_rec(5, 90)
14+
(0.0, 5.0)
15+
>>> pol_to_rec(5, 180)
16+
(-5.0, 0.0)
17+
>>> pol_to_rec(5, 270)
18+
(-0.0, -5.0)
19+
>>> pol_to_rec(5, 360)
20+
(5.0, -0.0)
21+
"""
22+
23+
rad = math.radians(mag)
24+
25+
real = round((mod * math.cos(rad)), 2)
26+
img = round(mod * math.sin(rad), 2)
27+
28+
rec = (real, img)
29+
30+
return rec
31+
32+
33+
if __name__ == "__main__":
34+
import doctest
35+
36+
doctest.testmod()

0 commit comments

Comments
 (0)