Search tan reverse in python

I am trying to find the angle created by the line connecting the point with the x axis and the x axis. Thus, I am trying to find a simple old tan-reverse. Here is the code I use in Python 3

angle_with_x_axis = math.atan(y_from_centre / x_from_centre)

I load point(1,1)like y_from_centreu x_from_centreand i get

0.7853981633974483

My expected result is 45, but naturally. What am I doing wrong here?

+4
source share
3 answers

mathuses radians. For degrees use math.degrees:

>>> math.degrees(math.atan(1))
45.0
+4
source

The math module works in radians. 0.785 radians is 45 degrees. From the docs:

math.atan (x)

Return the arc tangent to x in radians.

+8
source

math.atan() - math - . :

Return the arc tangent to x, in radians .

(my emphasis)

However, the module mathprovides a way to convert radians to degrees and vice versa:

>>> import math
>>> math.degrees(math.atan(1))
45.0
>>> 
>>> math.radians(45.0)
0.7853981633974483
>>> 

You can also create a helper function to carry this logic:

>>> def atan_in_degress(x):
...     return math.degrees(math.atan(x))
... 
>>> atan_in_degress(1)
45.0
>>> atan_in_degress(2)
63.43494882292202
>>> 
+2
source

Source: https://habr.com/ru/post/1666104/


All Articles