在python中计算对数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33754670/
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
Calculate logarithm in python
提问by Nasser
I am wondering why the result of log base 10 (1.5)
in python = 0.405465108108 while the real answer = 0.176091259.
我想知道为什么log base 10 (1.5)
python的结果= 0.405465108108 而真正的答案 = 0.176091259。
This is the code that I wrote:
这是我写的代码:
import math
print math.log(1.5)
Can someone tell how to solve this issue?
有人可以告诉如何解决这个问题吗?
采纳答案by Ignacio Vazquez-Abrams
From the documentation:
从文档:
With one argument, return the natural logarithm of x(to base e).
With two arguments, return the logarithm of xto the given base, calculated as
log(x)/log(base)
.
使用一个参数,返回x的自然对数(以e为底)。
使用两个参数,返回x到给定底数的对数,计算为
log(x)/log(base)
。
But the log10is made available as math.log10()
, which does not resort to log division if possible.
但是日志10作为 可用math.log10()
,如果可能,它不会求助于日志划分。
回答by utkbansal
math.log10(1.5)
Use the log10 function in the math module.
使用数学模块中的 log10 函数。
回答by utkbansal
The math.log
function is to the base e
, i.e. natural logarithm. If you want to the base 10 use math.log10
.
该math.log
功能是基础e
,即自然对数。如果你想以 10 为基数使用math.log10
.
回答by Altoyyr
If you use log without base it uses e
.
如果您使用没有 base 的 log,它将使用e
.
From the comment
来自评论
Return the logarithm of x to the given base.
If the base not specified, returns the natural logarithm (base e) of x.
返回 x 的对数到给定的底数。
如果未指定底数,则返回 x 的自然对数(底数 e)。
Therefor you have to use:
因此你必须使用:
import math
print( math.log(1.5, 10))