Python atan或atan2,我应该使用什么?

2024-07-05 10:30:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我的公式 f=电弧(ImZ/ReZ)

有两种选择:

选项1(atan):

ImZ=-4.593172163003
ImR=-4.297336384845

>>> z=y/x
>>> f1=math.atan(z)
>>> f1
0.8186613519278327

选项2(atan2)

>>> f=math.atan2(y,x)
>>> f
-2.3229313016619604

为什么这两个结果不同?


Tags: 选项mathrez公式f1atanatan2imr
3条回答

math.atan的docstring:

atan(x) Return the arc tangent (measured in radians) of x.

math.atan2的docstring:

atan2(y, x) Return the arc tangent (measured in radians) of y/x. Unlike atan(y/x), the signs of both x and y are considered.

为了完整起见,以下是医生对atan2的看法:

math.atan2(y, x) Return atan(y / x), in radians. The result is between -pi and pi. The vector in the plane from the origin to point (x, y) makes this angle with the positive X axis. The point of atan2() is that the signs of both inputs are known to it, so it can compute the correct quadrant for the angle. For example, atan(1) and atan2(1, 1) are both pi/4, but atan2(-1, -1) is -3*pi/4.

所以很明显:输出是不同的,因为有ImZImR的符号。atan2返回适当的象限,与atan不同。

Atan采用单参数,Atan2采用两个参数。使用两个参数而不是一个参数的目的是收集有关输入符号的信息,以便返回计算角度的适当象限,这对于单参数Atan是不可能的

enter image description here

Atan2结果总是介于-pi和pi之间。

参考: https://en.wikipedia.org/wiki/Atan2

从0开始逆时针方向的角度,也就是x的正轴

对于x和y的任何值为2pi。对于x=y=0,结果未定义。

f(x,y)=pi()-pi()/2*(1+符号(x))*(1-符号(y^2))-pi()/4*(2+符号(x))*符号(y)

    -sign(x*y)*atan((abs(x)-abs(y))/(abs(x)+abs(y)))

相关问题 更多 >