Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8659247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:58:08+00:00 2026-06-12T15:58:08+00:00

I understand that this has been solved in C/C++, but I am not comfortable

  • 0

I understand that this has been solved in C/C++, but I am not comfortable enough with those languages to be able to convert it to python. I am trying to create this in python. The closest I was able to come was this:

#This is meant to work for functions of the form x^a + b = 0

def secant(base, exp=2, it=20):
    def f(x):
        return x**exp - base
    x1 = base / float(exp**2)
    xnm1 = x1 - 5
    xnm2 = x1 + 5
    xn = 0
    for n in range(it):
        q = (xnm1-xnm2)/float(f(xnm1)-f(xnm2))
        xn = xnm1 - (f(xnm1)*q)
        xnm1, xnm2 = xn, xnm1
    return xn

print secant(2, 2)

This returns the error:

Traceback (most recent call last):
  File "/Users/Joe/Desktop/secant.py", line 16, in <module>
    print secant(2, 2)
  File "/Users/Joe/Desktop/secant.py", line 11, in secant
    q = (xnm1-xnm2)/float(f(xnm1)-f(xnm2))
ZeroDivisionError: float division by zero

I was able, however, to program the Newton method, which I based this code off of. If it helps, here it is:

def newton(base, exp=2, it=20):
    def f(x):
        return x**exp - base
    def df(x):
        return exp*(x**(exp-1))
    x1 = base / float(exp**2)
    xnp = x1
    xn = 0
    for n in range(it):
        xn = xnp - ((f(xnp)/df(xnp)))
        xnp = xn
    return xn

The following method gives an answer with 12 digits of accuracy after 20 iterations. Any help would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T15:58:10+00:00Added an answer on June 12, 2026 at 3:58 pm

    You are getting a division by zero error because the algorithm converged to the answer within the precision of Python floats. In addition to iterating a maximum number of times (to avoid an infinite loop), you should also check if the last two guesses are “close enough”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand this simple hashlib code in Python that has been
This problem must have been solved a million times, but Google has not been
I understand that this question has been done to death so I'll apologise in
I understand this question has been asked before, but I couldn't think of a
I understand that 2 options are available: Non-persistent Persistent But what does this actually
This probably has probably been asked before, but I couldn't find anything relevant. Would
My question has already been asked here , but I am trying to understand
I've been working on this problem for quite a while but have not been
I understand that this question may be subjective, this is why I need an
I understand that this question could be answered with a simple sentence and that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.