为什么 Python 会抛出“OverFlow”?浮点计算异常?
我刚刚开始学习 Python(抱歉,不要在这方面做作业,因为它不是)。只是为了给自己做一些有意义的练习,以更好地掌握语法和功能,我一直在关注以下 URL:http://www.cs.washington.edu/homes/stepp/bridge/2007/exercises.html
我收到的这个特殊的“溢出”问题一些浮点计算让我感到困惑。
这是我收到的错误消息:
Traceback (most recent call last):
File "./lab0102.py", line 28, in <module>
payment = PMT(r, n, P)
File "./lab0102.py", line 19, in PMT
return round(P * ((r *((1+r)**num_pmts)) / ((1+r)**num_pmts)))
OverflowError: (34, 'Numerical result out of range')
这是我的代码:
import math
#from decimal import Decimal
def PMT(r, n, P):
rate = (r/100)/12
print "rate:", rate
num_pmts = n*12
payment = P * ((r *((1+r)**num_pmts)) / ((1+r)**num_pmts))
return payment
print "This program computes monthly loan payments."
P = input("Loan Amount? ")
n = input("Number of Years? ")
r = input("Interest Rate? ")
payment = PMT(r, n, P)
print "You payment is", payment
我已经通过尝试对输入进行类型转换、使用一些包装器操作来舍入或指定小数点精度来完成所有操作。我什至使用 Decimal 模块尝试以字符串格式打印出小数,以查看我的逻辑缺陷在哪里。
有没有人愿意教我Python 中的浮点计算领域?
I'm just starting to get into learning Python (Sorry, don't claim homework on this one, because it's not). Just to give myself some meaningful exercises to do to get better with the syntax and features, I've been following this URL: http://www.cs.washington.edu/homes/stepp/bridge/2007/exercises.html
This particular 'Overflow' issue I'm receiving with some floating point calculations is just mystifying me.
This is the error message I get:
Traceback (most recent call last):
File "./lab0102.py", line 28, in <module>
payment = PMT(r, n, P)
File "./lab0102.py", line 19, in PMT
return round(P * ((r *((1+r)**num_pmts)) / ((1+r)**num_pmts)))
OverflowError: (34, 'Numerical result out of range')
Here's my code:
import math
#from decimal import Decimal
def PMT(r, n, P):
rate = (r/100)/12
print "rate:", rate
num_pmts = n*12
payment = P * ((r *((1+r)**num_pmts)) / ((1+r)**num_pmts))
return payment
print "This program computes monthly loan payments."
P = input("Loan Amount? ")
n = input("Number of Years? ")
r = input("Interest Rate? ")
payment = PMT(r, n, P)
print "You payment is", payment
I've done everything by trying to typecast input, to use some of the wrapper operations to round or specify decimal point precision. I've even used the Decimal module to try and print out the decimal in string format to see where my logic flaw is.
Any takers on this one to educate me in the realm of floating point calculations in Python?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
input()
仅当它确实看到看起来像浮点数的东西时才给你一个浮点数;如果它看起来像一个整数,那么你将得到一个整数。请改用float(raw_input())
。现在,关于溢出。
旧版本的 Python 不会自动将
int
提升为long
,这意味着您可以拥有的最大整数是有符号 32 位或 64 位;任何高于该值都会导致溢出。由于表达式中有整数(见上文),因此可能会违反类型的最大值,从而导致您看到的异常。input()
only gives you a float if it actually sees something that looks like a float; if it looks like an integer then you will get an integer. Usefloat(raw_input())
instead.Now, as to the overflow.
Older versions of Python did not promote
int
tolong
automatically, meaning that the maximum integer you can have is signed 32- or 64-bit; any higher than that results in an overflow. Since you have integers in your expression (see above), you can potentially breach the maximum value for the type, resulting in the exception you're seeing.