我如何在python 2.7中使用“e”(欧拉数)和幂运算
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39154611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can i use "e" (Euler's number) and power operation in python 2.7
提问by Thanos Smar
How can i write x.append(1-e^(-value1^2/2*value2^2))
in python 2.7?
我如何x.append(1-e^(-value1^2/2*value2^2))
在 python 2.7 中编写?
I don't know how to use power operator and e.
我不知道如何使用电源运算符和 e。
回答by Moinuddin Quadri
Refer mathlibrary of python. exp(x)
function this library is same as e^x
. Hence you may write your code as:
参考python的数学库。exp(x)
此库的功能与e^x
. 因此,您可以将代码编写为:
I have modified the equation by replacing 1/2
as 0.5
. Else for Python <2.7we have to explicitly type cast the division value to float
because Python round of the result of division of two int
. (for example: 1/2 -> 0
in python 2.7 and below)
我通过替换1/2
as修改了方程0.5
。否则对于 Python <2.7,我们必须明确地将除法值转换为类型,float
因为 Python 是对二除法结果的舍入int
。(例如:1/2 -> 0
在 python 2.7 及以下版本中)
import math
x.append(1 - math.exp( -0.5 * (value1*value2)**2))
回答by karlosss
Python's power operator is **
and Euler's number is math.e
, so:
Python 的幂运算符是**
,欧拉数是math.e
,所以:
from math import e
x.append(1-e**(-value1**2/2*value2**2))
回答by Daniel
Power is **
and e^
is math.exp
:
功率是**
和e^
是math.exp
:
x.append(1 - math.exp(-0.5 * (value1*value2)**2))
回答by jorijnsmit
Just saying: numpy
has this too. So no need to import math
if you already did import numpy as np
:
只想说:numpy
这个也有。因此,math
如果您已经这样做了,则无需导入import numpy as np
:
>>> np.exp(1)
2.718281828459045
回答by Alexey K.
math.e or from math import e (2.718281…)
math.e 或 from math import e (2.718281…)
math.exp(x) = e**x
however:
Return e raised to the power x, where e = 2.718281… is the base of natural logarithms. This is usually more accurate than math.e ** x or pow(math.e, x). docs.python
math.exp(x) = e**x
然而:
返回 e 的 x 次幂,其中 e = 2.718281…是自然对数的底。这通常比 math.e ** x 或 pow(math.e, x) 更准确。文档.python
for power use " ** " (3**2 = 9), not " ^ "
" ^ " is a bitwise XOR operator (& and, | or), it works logicaly with bits 10^4=14 (0000 1010 ^ 0000 0100 = 0000 1110) programiz
对于电源使用“**”(3**2 = 9),而不是“^”
“^”是按位异或运算符(&和,|或),它在逻辑上与位10^4=14(0000 1010 ^ 0000 0100 = 0000 1110) 程序化