我\xc2\xb4m 是编程新手,所以我不知道出了什么问题。请帮忙
\n\nfrom 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)\nRun Code Online (Sandbox Code Playgroud)\n
在 Python 中,有 2 个反正切函数:atan是 的倒数tan;但atan2需要 2 个参数。在你的情况下,因为你知道两种catheti,你也可以使用 2-argument 函数atan2:
ang = atan2(y, x)\nRun Code Online (Sandbox Code Playgroud)\n\n或者,你可以写
\n\nang = atan(y / x)\nRun Code Online (Sandbox Code Playgroud)\n\n其基本原理atan2是,即使 为x0,它也能正常工作;而与atan(y / x)aZeroDivisionError: float division by zero会被提高。
另外,atan只能给出 -\xcf\x80/2 ... +\xcf\x80/2 之间的角度,而知道和atan2的符号,从而可以知道该值属于 4 个象限中的哪一个;其值范围为 -\xcf\x80 到 +\xcf\x80。不过,当然你不会有一个宽度或高度为负的三角形......yx
| 归档时间: |
|
| 查看次数: |
3232 次 |
| 最近记录: |