+ Reply to Thread
Results 1 to 7 of 7

Do Until Loop in finding the Square Root

  1. #1
    Registered User
    Join Date
    04-29-2012
    Location
    Juperos Elevator
    MS-Off Ver
    Excel 2007
    Posts
    10

    Do Until Loop in finding the Square Root

    Hello! I need a little help. I want to make a code that would use the Babylonian method of finding the square root

    This one's a short explanation from wikipedia:

    ScreenShot228.jpg

    And here's my scratch code: I can't seem to make it work.
    Please Login or Register  to view this content.
    What do I need to change? I also want to show the answer with a Message Box. Thank you!
    Last edited by Leith Ross; 05-06-2012 at 06:08 PM. Reason: Added Code Tags

  2. #2
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: Do Until Loop in finding the Square Root

    Perhaps something like this:

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    04-29-2012
    Location
    Juperos Elevator
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Do Until Loop in finding the Square Root

    Wow, thank you very much! :D One last question though, would the do until loop also work for this, or only the for loop works? :D Thanks again!

  4. #4
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: Do Until Loop in finding the Square Root

    One last question though, would the do until loop also work for this, or only the for loop works?
    Actually, I just printed out your screen shot and worked from there.

    But, there is a problem with: Do Until ctr = S ^ (1 / 2)
    (1) You made all your variables integers (which means they will always be whole numbers).
    As an aside, IMO if you want to work with integers, use long (integers) and not (short) integers.
    "As Integer" makes a variable a short integer; "As Long" makes it a long integer.
    Because ctr is an integer, ctr will never equal S ^ (1 / 2)

    (2) Even if you made ctr "As Double", you might still have a problem. Because of Floating Point Precision.
    The way around this would be to Round both numbers to so many digits after the decimal point.
    But I would have to play around with the code to be certain.

  5. #5
    Registered User
    Join Date
    04-29-2012
    Location
    Juperos Elevator
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Do Until Loop in finding the Square Root

    Ohh, I finally understand! The do until is much better. I'll try to use Round with the do until and see if it works. Thank you so much! :D

  6. #6
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: Do Until Loop in finding the Square Root

    If I'm not mistaken, one cannot get more than 15 significant digits out of a double.

  7. #7
    Registered User
    Join Date
    04-29-2012
    Location
    Juperos Elevator
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Do Until Loop in finding the Square Root

    Oops, I intended to say "the for loop code is much better". Thanks a lot for the help!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1