Python 中的 ln(自然日志)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39235906/
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
ln (Natural Log) in Python
提问by Michael Watts
In this assignment I have completed all the problems except this one. I have to create a python script to solve an equation (screenshot).
在这个作业中,我已经完成了除了这个问题之外的所有问题。我必须创建一个 python 脚本来求解方程(截图)。
Unfortunately, in my research all over the internet I cannot figure out how in the world to either convert ln to log or anything usable, or anything. The code I have written so far is below. I will also post the answer that our teacher says we should get.
不幸的是,在我在整个互联网上的研究中,我无法弄清楚如何将 ln 转换为 log 或任何可用的东西,或任何东西。到目前为止,我编写的代码如下。我也会贴出我们老师说我们应该得到的答案。
import math
p = 100
r = 0.06 / 12
FV = 4000
n = str(ln * ((1 + (FV * r) / p) / (ln * (1 + r))))
print ("Number of periods = " + str(n))
The answer I should get is 36.55539635919235 Any advice or help you have would be greatly appreciated!
我应该得到的答案是 36.55539635919235 任何建议或帮助将不胜感激!
Also, we are not using numpy. I already attempted that one.
此外,我们没有使用 numpy。我已经尝试过那个。
Thanks!
谢谢!
回答by Diziet Asahi
math.log
is the natural logarithm:
math.log
是自然对数:
math.log(x[, base]) With one argument, return the natural logarithm of x (to base e).
math.log(x[, base]) 使用一个参数,返回 x 的自然对数(以 e 为底)。
Your equation is therefore:
因此,您的等式是:
n = math.log((1 + (FV * r) / p) / math.log(1 + r)))
Note that in your code you convert n to a str
twice which is unnecessary
请注意,在您的代码中,您将 n 转换为str
两次,这是不必要的
回答by Grant Shannon
Here is the correct implementation using numpy(np.log()
is the natural logarithm)
这是使用numpy的正确实现(np.log()
是自然对数)
import numpy as np
p = 100
r = 0.06 / 12
FV = 4000
n = np.log(1 + FV * r/ p) / np.log(1 + r)
print ("Number of periods = " + str(n))
Output:
输出:
Number of periods = 36.55539635919235