【问题标题】:Unusual Math with incorrect results? [duplicate]不寻常的数学结果不正确? [复制]
【发布时间】:2012-01-01 04:10:42
【问题描述】:

当我使用 math.cos() 和 math.sin() 函数时,我的 python 解释器表现得很时髦。例如,如果我在我的计算器上这样做:

cos(35)*15+9 = 21.28728066
sin(35)*15+9 = 17.60364655

但是当我在 python(3.2 和 2.7)上这样做时

>>> import math
>>> math.cos(35)*15+9
-4.5553830763726015

>>> import math
>>> math.sin(35)*15+9
2.577259957557734

为什么会这样?

编辑:如何将 Python 中的弧度更改为度数,以防万一?

【问题讨论】:

  • 您的计算器可能使用度数(35 度)。将其更改为使用辐射,它的行为将与 Python 数学模块相同。数学家更喜欢辐射度而不是(周期的任意 1/360)度数。
  • 我会给你+1,因为你觉得这种行为很时髦。数学可以很酷:)
  • @ypercube: It's explicit in the documentation 不过,所有三角函数的单位都是弧度。
  • @Makoto:我有没有说别的?
  • 真正的数学家会嘲笑那些在三角学中使用度数的人:-)

标签: python math trigonometry


【解决方案1】:

这是由于您使用的是 degrees
并且三角函数期望 radians 作为输入:
sin(radians)

sin的描述是:

sin(x)
    Return the sine of x (measured in radians).

在 Python 中,您可以使用 math.radians 函数将度数转换为弧度。


因此,如果您使用输入进行此操作:

>>> math.sin(math.radians(35)) * 15 + 9
17.60364654526569

它给出的结果与您的计算器相同。

【讨论】:

  • 好的,谢谢您的回答! +1
【解决方案2】:

以下 Python 方法可用于将弧度转换为度数,反之亦然:

math.degrees
math.radians

使用您的示例:

>>> from math import cos, sin, radians
>>> sin(radians(35)) * 15 + 9
17.60364654526569
>>> cos(radians(35)) * 15 + 9
21.28728066433488

奇怪的计算器...取度但返回弧度。

【讨论】:

  • 没有任何可以以度或弧度测量的角度值返回。
【解决方案3】:
cos(35)[degree] = 0.819 * 15 + 9 = 21.285  <-- Your calculator

sin(35)[radian] = -0.428 * 15 + 9 = 2.577  <-- Python

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-03
    • 1970-01-01
    • 1970-01-01
    • 2014-10-20
    • 1970-01-01
    相关资源
    最近更新 更多