为什么python会给出这样一个错误;浮点数除以零“;“在评估时”;日志(e,1)“;
我定义了一个函数,它是为什么python会给出这样一个错误;浮点数除以零“;“在评估时”;日志(e,1)“;,python,math,logarithm,Python,Math,Logarithm,我定义了一个函数,它是 from math import * def func(x): return log(e,x) 错误简短明了,您知道为什么python无法计算吗 func(1) ,等于ln(1) 编辑:因为我是新来的,我发布了一个愚蠢的问题,对不起,但现在我处理了它@mike_z关于他推荐的内容是正确的 我有一个反向参数,换句话说,我想到了函数 math.log(a,b)或log(a,b) (取决于您如何导入数学模) 就好像a表示基数,b表示要计算其对数的另一个操作数一样
from math import *
def func(x):
return log(e,x)
错误简短明了,您知道为什么python无法计算吗
func(1)
,等于ln(1)
编辑:因为我是新来的,我发布了一个愚蠢的问题,对不起,但现在我处理了它@mike_z关于他推荐的内容是正确的 我有一个反向参数,换句话说,我想到了函数
math.log(a,b)
或log(a,b)
(取决于您如何导入数学模)
就好像a表示基数,b表示要计算其对数的另一个操作数一样
但真正的方法是上面句子的倒数是对的
加油 @mike_z的建议是对的 我有一个反向参数,换句话说,我想到了函数
math.log(a,b)
或log(a,b)
(取决于您如何导入数学模)
就好像a表示基数,b表示要计算其对数的另一个操作数一样
但真正的方法是上面句子的倒数是对的
加油 这是
log
函数的定义。如评论中所述,您的基数不能是1
def log(x, base=None): # real signature unknown; restored from __doc__
"""
log(x[, base])
Return the logarithm of x to the given base.
If the base not specified, returns the natural logarithm (base e) of x.
"""
pass
您可以看到以下操作:-
>>> import math
>>>
>>> def func(x):
...
... return math.log(10, x)
...
>>> print func(1)
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "<input>", line 3, in func
ZeroDivisionError: float division by zero
>>> print func(2)
3.32192809489
>>> print func(3)
2.09590327429
>>> print func(4)
1.66096404744
>>>
导入数学
>>>
>>>def func(x):
...
... 返回math.log(10,x)
...
>>>打印功能(1)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“”,第3行,在func中
ZeroDivisionError:浮点除以零
>>>打印功能(2)
3.32192809489
>>>打印功能(3)
2.09590327429
>>>打印功能(4)
1.66096404744
>>>
这是
log
函数的定义。如评论中所述,您的基数不能是1
def log(x, base=None): # real signature unknown; restored from __doc__
"""
log(x[, base])
Return the logarithm of x to the given base.
If the base not specified, returns the natural logarithm (base e) of x.
"""
pass
您可以看到以下操作:-
>>> import math
>>>
>>> def func(x):
...
... return math.log(10, x)
...
>>> print func(1)
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "<input>", line 3, in func
ZeroDivisionError: float division by zero
>>> print func(2)
3.32192809489
>>> print func(3)
2.09590327429
>>> print func(4)
1.66096404744
>>>
导入数学
>>>
>>>def func(x):
...
... 返回math.log(10,x)
...
>>>打印功能(1)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“”,第3行,在func中
ZeroDivisionError:浮点除以零
>>>打印功能(2)
3.32192809489
>>>打印功能(3)
2.09590327429
>>>打印功能(4)
1.66096404744
>>>
假设,您认为log(e,1)的计算结果应该是什么?您不能使用
1
作为对数的基数log(x,b)
是必须提高b
才能获得x
的指数。但是当你将1
提升到任何指数时,结果总是1
。你定义了什么函数?你能给我们看看你函数的代码吗?你的参数倒过来了。基数是第二个参数。假设,你认为log(e,1)的计算结果应该是什么?你不能用1
作为对数的基数log(x,b)
是必须提高b
才能获得x
的指数。但是当你将1
提升到任何指数时,结果总是1
。你定义了什么函数?你能给我们看看你函数的代码吗?你的参数倒过来了。基础是第二个论点。