MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / rectangular_to_polar

Function rectangular_to_polar

conversions/rectangular_to_polar.py:4–26  ·  view source on GitHub ↗

https://en.wikipedia.org/wiki/Polar_coordinate_system >>> rectangular_to_polar(5,-5) (7.07, -45.0) >>> rectangular_to_polar(-1,1) (1.41, 135.0) >>> rectangular_to_polar(-1,-1) (1.41, -135.0) >>> rectangular_to_polar(1e-10,1e-10) (0.0, 45.0) >>> rectangular_t

(real: float, img: float)

Source from the content-addressed store, hash-verified

2
3
4def rectangular_to_polar(real: float, img: float) -> tuple[float, float]:
5 """
6 https://en.wikipedia.org/wiki/Polar_coordinate_system
7
8 >>> rectangular_to_polar(5,-5)
9 (7.07, -45.0)
10 >>> rectangular_to_polar(-1,1)
11 (1.41, 135.0)
12 >>> rectangular_to_polar(-1,-1)
13 (1.41, -135.0)
14 >>> rectangular_to_polar(1e-10,1e-10)
15 (0.0, 45.0)
16 >>> rectangular_to_polar(-1e-10,1e-10)
17 (0.0, 135.0)
18 >>> rectangular_to_polar(9.75,5.93)
19 (11.41, 31.31)
20 >>> rectangular_to_polar(10000,99999)
21 (100497.76, 84.29)
22 """
23
24 mod = round(math.sqrt((real**2) + (img**2)), 2)
25 ang = round(math.degrees(math.atan2(img, real)), 2)
26 return (mod, ang)
27
28
29if __name__ == "__main__":

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected