python 蟒蛇对数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/961972/
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
python logarithm
提问by Hick
i want to find out a log10 of an integer in python and i get an error like math domain error
我想在 python 中找出一个整数的 log10 并且我得到一个像数学域错误这样的错误
my code is this w=math.log10(q*q1)/math.log10(2)
我的代码是这个 w=math.log10(q*q1)/math.log10(2)
where q1,q2 are integers
其中 q1,q2 是整数
yeah q1 is 0 sometimes
是的 q1 有时是 0
回答by Laurence Gonsalves
You can only compute the logarithm of a positive number. Trying to compute the logarithm for a negative number or zero will result in a "math domain error" in Python.
您只能计算正数的对数。尝试计算负数或零的对数将导致 Python 中的“数学域错误”。
By the way: it looks like you're actually trying to compute a logarithm base 2. You can do this with math.log
:
顺便说一句:看起来您实际上是在尝试计算以 2 为底的对数。您可以使用以下方法执行此操作math.log
:
w=math.log(q*q1, 2)
The second, optional, parameter is the base. It defaults to e (ie: natural log).
第二个可选参数是基数。它默认为 e(即:自然对数)。
回答by Mitch Wheat
Is q or q1 equal to zero or one of them negative?
q 或 q1 等于零还是其中之一为负?
回答by jacob
math.log10(0) is minus infinity. See: http://en.wikipedia.org/wiki/Logarithm
math.log10(0) 是负无穷大。请参阅:http: //en.wikipedia.org/wiki/Logarithm
回答by Arnav_Garg
try to make sure the value whose log you are trying to find can never be 0. As log(0) tends to negative infinity, the function call will give you a math domain error. Correct that and I think you'll be fine.
尝试确保您尝试查找其日志的值永远不会为 0。由于 log(0) 趋于负无穷大,函数调用会给您一个数学域错误。纠正一下,我想你会没事的。