-4

每当我输入此代码时,我都会OverflowError: math range error在第 4 行得到一个。我如何解决它?

x=0
while True:
    x=int(x)+1
    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)
    if x==30:
        break
4

1 回答 1

0

The simple answer is that the equation you are using does not have a root. you can see this by using a print first_root statement inside your while loop and seeing that it diverges to +-inf.

However, assuming your textbook isn't screwing with you, and the equation does have a root I can almost guarantee that you have a parenthesis in the wrong spot. Below is your exact code with all of the unneeded parenthesis removed and first_root replaced with r to make it easier to read.

r = r - a*pow(r, 3) + b*(pow(r, 2) + c*r + d) / 3*a*pow(r, 2) + 2*b*r + c

What you need to do is compare this with the equation in your book and see where/if you messed it up.

于 2013-06-02T19:08:23.907 回答