【问题标题】:Calculate a simple expression in python在python中计算一个简单的表达式
【发布时间】:2020-12-13 12:22:38
【问题描述】:

我想在python中计算(π/4)^2-sin⁡(π/4):

import math
a=((math.pi)^2/16)-math.sin(math.pi/4)
print(a)

它给出了错误:

TypeError: unsupported operand type(s) for ^: 'float' and 'float'                                                 

我也尝试过使用

a=float(((math.pi)^2/16)-math.sin(math.pi/4))

还是不行

【问题讨论】:

标签: python-3.x


【解决方案1】:

^ 在 python 中不受支持。相反,您可以使用math.pow

import math
a=(math.pow((math.pi),2)/16.0)-math.sin(math.pi/4)
print(a)

【讨论】:

  • 我用了**,但它给出了错误的答案! **是什么问题?
  • 我也使用过 math.pow 它给出了同样的错误答案。它给出 -0.09025 但在计算器中它显示 0.6031
  • ** 与 power 2 的 pow 相同。我尝试使用 python(-0.09025650611846259) 和计算器(-0.09025650611) 执行相同的代码,我得到相同的响应,值最高为 11计算器中的数字。
  • 不知道为什么我的计算器会给出这个答案
  • 你可以清空你的计算器的记忆,然后再试一次。或者,使用在线计算器。共享的响应来自在线计算器。
【解决方案2】:

^ 在 python 中不受支持。相反,您可以使用 ma​​th.pow 或 **

math.pow(math.pi,2)

math.pi ** 2

【讨论】:

    【解决方案3】:

    两次计算 pi/4 没有意义。

    import math
    pi4 = math.pi/4
    a = pi4**2 - math.sin(pi4)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-02
      • 2020-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-27
      相关资源
      最近更新 更多