我收到错误,“atan2() 恰好需要 2 个参数(给定 1 个)”

Osw*_*les 2 python atan2

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

\n\n
from math import atan2, pi\nx = int(input("value of x"))\ny = int(input("value of y"))\nr = (x**2 + y**2) ** 0.5\nang = atan2(y/x)\nprint("Hypotenuse is", r, "angle is", ang)\n
Run Code Online (Sandbox Code Playgroud)\n

Ant*_*ala 5

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

\n\n
ang = atan2(y, x)\n
Run Code Online (Sandbox Code Playgroud)\n\n

或者,你可以写

\n\n
ang = atan(y / x)\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n\n

其基本原理atan2是,即使 为x0,它也能正常工作;而与atan(y / x)aZeroDivisionError: float division by zero会被提高。

\n\n

另外,atan只能给出 -\xcf\x80/2 ... +\xcf\x80/2 之间的角度,而知道和atan2的符号,从而可以知道该值属于 4 个象限中的哪一个;其值范围为 -\xcf\x80 到 +\xcf\x80。不过,当然你不会有一个宽度或高度为负的三角形......yx

\n