-1

I'm trying to find the fourth root of a number using the newton raphson method, and I get this weird error in my code. Can anyone please help me fix it?

Thanks

while abs(g**4-k)>=epsilon:
builtins.OverflowError: (34, 'Result too large')


def root():
epsilon = 0.01
k = 100
g = k/2.0
count = 1
while abs(g**4-k)>=epsilon:
    g = g-(((g**4)-k/(4*g**3)))
    count += 1
print("Approximate fourth root of", k, 'is about', g)
bot123
  • 1
  • 1
    Just a missing pair of parentheses: `g = g-((((g**4)-k)/(4*g**3)))` – Paul Panzer Oct 06 '19 at 01:50
  • @bot123, Good post. You included your code which many new contributors do not do. You should include the specific error you got. "Weird error" is not specific enough for most things in life when you are asking for help and Stack Overflow is no exception. Some advice for editing Python files: find a good text editor with syntax highlighting. Turn on the "show line numbers" option. If you are using Windows, I think it ships with Idle, which is OK. Most of these editors will highlight matching parentheses. – bfris Oct 06 '19 at 16:36
  • Most Python errors will give you a line number. If the line number looks fishy, you can often start going backwards and looking for parentheses completion. This happens to me ALL THE TIME. – bfris Oct 06 '19 at 16:38

1 Answers1

0

You have a mistake with your brackets in the next line after the while condition (close parentheses after the k). See code below:

def root():
    epsilon = 0.01
    k = 100
    g = k/2.0
    count = 1
    while abs(g**4-k) >= epsilon:
        g = g-(((g**4)-k)/(4*g**3))
        count += 1
    print("Approximate fourth root of", k, 'is about', g)
wgb22
  • 235
  • 1
  • 10