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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35749246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 16:54:50  来源:igfitidea点击:

Python atan or atan2, what should I use?

pythonmath

提问by Richard Rublev

My formula f=arctan(ImZ/ReZ)

我的公式 f=arctan(ImZ/ReZ)

There are two options:

有两种选择:

Option 1 (atan):

选项 1(atan):

ImZ=-4.593172163003
ImR=-4.297336384845

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

Option 2 (atan2)

选项 2 (atan2)

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

Why are these two results different?

为什么这两个结果不同?

回答by Abdul Rauf

Atan takes single argument and Atan2 takes two arguments.The purpose of using two arguments instead of one is to gather information on the signs of the inputs in order to return the appropriate quadrant of the computed angle, which is not possible for the single-argument Atan

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

enter image description here

在此处输入图片说明

Atan2 result is always between -pi and pi.

Atan2 结果总是在 -pi 和 pi 之间。

Reference: https://en.wikipedia.org/wiki/Atan2

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

回答by DevShark

docstring for math.atan:

math.atan 的文档字符串:

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

atan(x) 返回 x 的反正切(以弧度为单位)。

docstring for math.atan2:

math.atan2 的文档字符串:

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(y, x) 返回 y/x 的反正切(以弧度为单位)。与 atan(y/x) 不同,x 和 y 的符号都被考虑。

To be very complete, here's what the doc says about atan2:

为了非常完整,以下是文档关于 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.

math.atan2(y, x) 返回 atan(y / x),以弧度为单位。结果介于 -pi 和 pi 之间。平面中从原点到点 (x, y) 的向量与正 X 轴形成这个角度。atan2() 的要点是它知道两个输入的符号,因此它可以计算角度的正确象限。例如,atan(1) 和 atan2(1, 1) 都是 pi/4,但 atan2(-1, -1) 是 -3*pi/4。

So it's pretty clear: the outputs are different because of the signs of ImZand ImR. atan2returns the appropriate quadrant, unlike atan.

所以,这是很明确的:由于输出的标志是不同的ImZImRatan2返回适当的象限,不像atan

回答by theodore panagos

A formula to have an angle counter clockwise from 0,that is the positive axis of x,

一个公式从 0 逆时针方向有一个角度,即 x 的正轴,

to 2pi for any value of x and y. For x=y=0 the result is undefined.

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

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

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

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