2

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?

3
  • What input values are you giving to your program? Commented Aug 25, 2011 at 0:43
  • Same from the URL I was getting the coding example from. P=275000, n=30, r=6.75. I did find my flaws. I was referencing passed in value 'r' in the calculation when I should have been using 'rate' instead. I also wasn't subtracting 1 from the denominator. Attention to detail on the formula helps in my case... O_o Commented Aug 25, 2011 at 1:00
  • What Every Programmer Should Know About Floating-Point Arithmetic. Are the numbers you're intending to compute larger than 2^1024 ≈ 1.8*10^308? Commented Aug 25, 2011 at 4:15

1 Answer 1

2

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. Use float(raw_input()) instead.

Now, as to the overflow.

Older versions of Python did not promote int to long 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.

Sign up to request clarification or add additional context in comments.

3 Comments

Sure, that's good to know. I wasn't aware of that with input(). I did find my flaws. Total logic errors in the formula that calculates PMT. I was referencing 'r' in the function when I should have been using 'rate' and I had some parens jacked up as well, so also fighting order of operation. Attention to detail I guess...
@adosch You shouldn't use input() in Python 2.x anyway, since it's equivalent to eval(raw_input(prompt)), and that's ... bad.
"Older versions" are pretty old. I don't recall it ever not promoting, and I started with 2.3.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.