简体   繁体   English

python数学的基本问题

[英]Basic troubles with python maths

Working on a very simple program to learn python.正在编写一个非常简单的程序来学习 Python。

The goal is to calculate Euler's constant with the user inputing a value for the equations precision (p).目标是通过用户输入方程精度 (p) 的值来计算欧拉常数。 The program should use that variable in the equation (1+1/p)^(10^p).程序应该在等式 (1+1/p)^(10^p) 中使用该变量。

I am currently having two problems with this program.我目前有这个程序的两个问题。 The first of which is that the program will work if I first define p as p=10**p, then use the equation (1+1/p)**p which brings us to the second problem.第一个是,如果我首先将 p 定义为 p=10**p,然后使用方程 (1+1/p)**p,那么程序将起作用,这将我们带到了第二个问题。

The second problem is that any (user inputted) value for p that exceeds 14 will not work at all.第二个问题是任何(用户输入的)p 值超过 14 都将不起作用。 (15 returns 3.035035... and higher values just return 1.0) (15 返回 3.035035... 更高的值只返回 1.0)

I am extremely new to python and its math syntax and that may be my problem.我对 python 及其数学语法非常陌生,这可能是我的问题。

Thank you all for any help谢谢大家的帮助

Code in question:有问题的代码:

import math
p=None
e=None

p = int(input('Please enter a whole number precison multiplier (1-14):'))

p=10.0**p

print((1.0+1.0/p)**p)

I guess you're trying to approximate e.我猜你正试图近似 e。 The formula is (1 + 1/p)^p as p -> infinity, not (1 + 1/p)^(10^p).公式是 (1 + 1/p)^p 作为 p -> 无穷大,而不是 (1 + 1/p)^(10^p)。 This should work:这应该有效:

p = int(input('Please enter a whole number precison multiplier:'))
print((1 + 1/p)**(p))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM