How Do I Fix This Overflowerror?
I keep getting a 'OverflowError: math range error'. No matter what I input, the result is the same. I'm running Python 3.3, and it's finding the problem at the last line. How do I
Solution 1:
I know you don't want to hear about your excessive use of parenthesis, but the problem is that you have the parenthesis in the wrong places. With the sheer number of parenthesis you used, it took a while to find the problem.
I think the following code is much cleaner, easier to debug, and vastly easier to maintain in the future. I also included what I think is the corrected version of your one-liner.
import math
a=float(input('a=?'))
b=float(input('b=?'))
c=float(input('c=?'))
d=float(input('d=?'))
critical_point_n=((-2*b)-math.sqrt(abs((4*(math.pow(b, 2)))-(12*a*c))))/(6*a)
first_root=critical_point_n-1if first_root==0and c==0:
first_root+=(-0.01)
for x in range(10):
f = a*first_root**3 + b*first_root**2 + c*first_root + d
fp = 3*a*first_root**2 + 2*b*first_root + c
first_root = first_root - (f/fp)
#first_root=first_root-(((a*(math.pow(first_root, 3)))+(b*(math.pow(first_root, 2))+(c*first_root)+d)))/((3*(a*(math.pow(first_root, 2))))+(2*(b*first_root))+c)
print(first_root)
Solution 2:
The math
range of functions work on doubles
... so you're out of range for that - re-write as normal Python floats which will scale as needs be, or look at using decimal.Decimal
which also has sqrt
, power
etc.. functions: http://docs.python.org/2/library/decimal.html
Post a Comment for "How Do I Fix This Overflowerror?"