Python/conversions/rectangular_to_polar.py
Julia Aragão a2be5adf67
Tests electronics/electric_conductivity.py #9943 (#12437)
* Function conversion rectangular number to polar

* #9943 : adding test to elelectronics/electric_conductivity.py

* updating DIRECTORY.md

* Apply suggestions from code review

* updating DIRECTORY.md

* Rename rec_to_pol.py to rectangular_to_polar.py

* updating DIRECTORY.md

* Update conversions/rectangular_to_polar.py

* Update conversions/rectangular_to_polar.py

---------

Co-authored-by: Julia <julia.de-jesus-aragao@imt-atlantique.net>
Co-authored-by: juliaaragao <juliaaragao@users.noreply.github.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
Co-authored-by: cclauss <cclauss@users.noreply.github.com>
2024-12-30 13:36:55 +01:00

33 lines
768 B
Python

import math
def rectangular_to_polar(real: float, img: float) -> tuple[float, float]:
"""
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_to_polar(-1e-10,1e-10)
(0.0, 135.0)
>>> rectangular_to_polar(9.75,5.93)
(11.41, 31.31)
>>> rectangular_to_polar(10000,99999)
(100497.76, 84.29)
"""
mod = round(math.sqrt((real**2) + (img**2)), 2)
ang = round(math.degrees(math.atan2(img, real)), 2)
return (mod, ang)
if __name__ == "__main__":
import doctest
doctest.testmod()