【问题标题】:I´m getting the error, "atan2() takes exactly 2 arguments (1 given)"我收到错误消息,“atan2() 正好需要 2 个参数(给定 1 个)”
【发布时间】:2016-12-28 09:43:45
【问题描述】:

我是编程新手,所以我不知道出了什么问题。请帮忙

from math import atan2, pi
x = int(input("value of x"))
y = int(input("value of y"))
r = (x**2 + y**2) ** 0.5
ang = atan2(y/x)
print("Hypotenuse is", r, "angle is", ang)

【问题讨论】:

  • 当您调用atan2 时,您需要传递2 个参数,y/x 是1 个数字,例如论据
  • 另外,检查atan2( 的右括号。

标签: python atan2


【解决方案1】:

该错误的原因是atan2 需要两个参数。观察:

>>> from math import atan, atan2
>>> atan(2)
1.1071487177940904
>>> atan2(4, 2)
1.1071487177940904

请注意,如果x 为零,atan(y/x) 将不起作用,但 atan2(y, x) 将继续正常工作:

>>> atan(4/0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero
>>> atan2(4, 0)
1.5707963267948966

【讨论】:

    【解决方案2】:

    在 Python 中,有 2 个反正切函数:atantan 的逆函数;但atan2 需要 2 个参数。在你的情况下,因为你知道两个catheti,你也可以使用2参数函数atan2

    ang = atan2(y, x)
    

    或者,你可以写

    ang = atan(y / x)
    

    atan2 的基本原理是即使 x 为 0 也能正常工作;而使用atan(y / x) 时,会引发ZeroDivisionError: float division by zero

    另外,atan 只能给出 -π/2 ... +π/2 之间的角度,而 atan2 知道 yx 的符号,因此可以知道哪个4象限值下降到;它的取值范围是 -π 到 +π。虽然,当然你不会有一个负宽度或高度的三角形......

    【讨论】:

      猜你喜欢
      • 2017-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-21
      • 1970-01-01
      • 2015-07-17
      • 2015-01-21
      • 1970-01-01
      相关资源
      最近更新 更多