Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Python not doing what I expect, no response. Help me, I`m new to Python

  1. #1
    Join Date
    Aug 2011
    Beans
    10
    Distro
    Ubuntu 11.04 Natty Narwhal

    Python not doing what I expect, no response. Help me, I`m new to Python

    I wrote a program to solve square root with the newton`s algorithm (square root of x, take a guess, keep doing g + x/g until it`s good enough. It is like this:

    x = input("i want the sqrrt of:")
    def average (a, b):
    av = (a+b)/2.0
    return av

    def improve (g, x):
    i = average (g, x/g)
    return i

    def ge (g, x):
    d = abs((g*g)-x)
    return (d == 0)

    def sqrroot (g, x):
    while (not ge (g, x)):
    improve (g, x)
    return g

    def sqrt (x):
    a = sqrroot (1, x)
    return a

    print sqrt (x)

    When I try to run it on terminal it does nothing ! it takes the first input and i need to open another terminal because it came useless, cant do nothing and it doesnt give me response.
    this program is working right if i do it direct on a python terminal, but i wanted to save it and run later

  2. #2
    Join Date
    Jul 2011
    Location
    /Europe/Netherlands
    Beans
    378
    Distro
    Kubuntu 22.04 Jammy Jellyfish

    Re: Python not doing what I expect, no response. Help me, I`m new to Python

    The problem must be this while loop:

    Code:
    while (not ge (g, x)):
            improve (g, x)
        return g
    I know next to nothing about math but it can only hang if the while condition is always true. A possible problem could be rounding in the improve() function, so it never actually improves. If you put floats in there they might get converted back to integers.

    To debug you could let each function print input and output.

  3. #3
    Join Date
    Aug 2011
    Beans
    10
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: Python not doing what I expect, no response. Help me, I`m new to Python

    I did what you said. Tried every function in separate, and when I saw the improve inside the while loop, I realized it kept improving the same numbers on a infinite loop ... So I put g = improve (g, x) , and the guess keeps improving all the way. Thanks man ! I'll post the program here shortly ...
    Last edited by matehussilvapb; September 14th, 2013 at 12:50 AM.

  4. #4
    Join Date
    Aug 2011
    Beans
    10
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: Python not doing what I expect, no response. Help me, I`m new to Python

    Here`s the program, it`s working correctly now.
    If you want to test it out, and maybe improve it, I`m great.
    Attached Files Attached Files

  5. #5
    Join Date
    Aug 2011
    Beans
    10
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: Python not doing what I expect, no response. Help me, I`m new to Python

    Quote Originally Posted by Euroman View Post
    The problem must be this while loop:

    Code:
    while (not ge (g, x)):
            improve (g, x)
        return g
    I know next to nothing about math but it can only hang if the while condition is always true. A possible problem could be rounding in the improve() function, so it never actually improves. If you put floats in there they might get converted back to integers.

    To debug you could let each function print input and output.
    Solved !

  6. #6
    Join Date
    Aug 2007
    Location
    UK
    Beans
    427
    Distro
    Ubuntu UNR

    Re: Python not doing what I expect, no response. Help me, I`m new to Python

    As Euroman pointed out, the code might hang due to limitations of the available numerical precision and rounding behaviours of the system as a whole. It may run perfectly on your machine for all possible floating point values and then again it might not.

    Code:
    import sys
    
    bound = sys.float_info.epsilon  # Value representing the largest rounding error.
    
    def ge (g, x):
        return -bound <= g * g - x <= bound

  7. #7
    Join Date
    Aug 2011
    Beans
    10
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: Python not doing what I expect, no response. Help me, I`m new to Python

    Quote Originally Posted by StephenF View Post
    As Euroman pointed out, the code might hang due to limitations of the available numerical precision and rounding behaviours of the system as a whole. It may run perfectly on your machine for all possible floating point values and then again it might not.

    Code:
    import sys
    
    bound = sys.float_info.epsilon  # Value representing the largest rounding error.
    
    def ge (g, x):
        return -bound <= g * g - x <= bound
    actually the code si working great with decimal numbers .... Can you explain this piece of a code ? Thanks

  8. #8
    Join Date
    Aug 2011
    Beans
    10
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: Python not doing what I expect, no response. Help me, I`m new to Python

    Quote Originally Posted by matehussilvapb View Post
    actually the code si working great with decimal numbers .... Can you explain this piece of a code ? Thanks
    Now I realized your point, because if you try the sqrt of 2 it creates an infinite loop, so I changed the function ge:
    Code:
    def ge
        d = abs(g ** 2 - x)
        return (d < 0.0001)
    Now it's working great but it can only approximate 3 decimal 'slots'.
    and just wondering ... if you can't create a 'loop counter' on python ?
    Last edited by matehussilvapb; September 15th, 2013 at 02:19 AM.

  9. #9
    Join Date
    Feb 2009
    Beans
    1,469

    Re: Python not doing what I expect, no response. Help me, I`m new to Python

    What's the point of ge, anyway? ge(a,b) is in no way an obvious abbreviation for abs(a**2-b) < precision. It's not clear why you would want to abbreviate that, anyway; the Pythonic approach would be to use it like that.

    Similarly, what's the point of improve? All it does is 1/2*(g + x/g); that's a simple expression and calling it "improve" is confusing, since it doesn't have any side effects (improving or otherwise).

    What I'm saying here is that you're going a little overboard with the functions. Finding a square root with Newton's method is a four-line algorithm at most. Keep your code simple, and keep the code that does one thing all in one place, and you'll find it much easier to troubleshoot.

    So, that said, if the precision isn't good enough, just use a smaller value instead of 0.0001. On my computer, sys.float_info.mant_dig is 53, so I should be able to get 53*lg(10) ≈ 15 decimal digits of precision just working with Python's floats.

    If you increase the precision too much, it'll start failing again; this is just a fact of working with floating-point numbers. If you want better precision than a float can offer, you might consider looking at the decimal.Decimal class, which is part of the Python standard library and supports arbitrary precision arithmetic.

    I'm not sure what exactly you mean by "loop counter"; naturally you can always initialize a variable outside a loop and increment it inside the loop if you want to know how many times it executes. Normally, though, there are better ways to do whatever it is you really want to do.

  10. #10
    Join Date
    Aug 2011
    Beans
    10
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: Python not doing what I expect, no response. Help me, I`m new to Python

    Quote Originally Posted by trent.josephsen View Post
    What's the point of ge, anyway? ge(a,b) is in no way an obvious abbreviation for abs(a**2-b) < precision. It's not clear why you would want to abbreviate that, anyway; the Pythonic approach would be to use it like that.

    Similarly, what's the point of improve? All it does is 1/2*(g + x/g); that's a simple expression and calling it "improve" is confusing, since it doesn't have any side effects (improving or otherwise).

    What I'm saying here is that you're going a little overboard with the functions. Finding a square root with Newton's method is a four-line algorithm at most. Keep your code simple, and keep the code that does one thing all in one place, and you'll find it much easier to troubleshoot.

    So, that said, if the precision isn't good enough, just use a smaller value instead of 0.0001. On my computer, sys.float_info.mant_dig is 53, so I should be able to get 53*lg(10) ≈ 15 decimal digits of precision just working with Python's floats.

    If you increase the precision too much, it'll start failing again; this is just a fact of working with floating-point numbers. If you want better precision than a float can offer, you might consider looking at the decimal.Decimal class, which is part of the Python standard library and supports arbitrary precision arithmetic.

    I'm not sure what exactly you mean by "loop counter"; naturally you can always initialize a variable outside a loop and increment it inside the loop if you want to know how many times it executes. Normally, though, there are better ways to do whatever it is you really want to do.
    Ok, man. I'll try to shorten it up. And that's what I wanted as a loop counter, but I actually realized it some days ago, you can just keep adding to a variable inside the loop.

Page 1 of 2 12 LastLast

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
  •