## tcc8m 3 years ago I am stuck on hand out 3 on an example using bisection search to find the square root of x (x=25). My issue: On the fourth iteration of the problem with x =25, low = 0, high = 6.25, ans = 3.125. 3.125*3.125 = 9.76 which is < 25. So now low = 3.125 and high =6.25. In the problem if ans*ans < x, low = ans. if ans*ans > x, high = ans. If both low and high map to same ans, how is it that in iteration 4 above that low = 3.125 and high = 6.25? More basic, why is there a local and global variable that are the same? Thanks! http://pastebin.com/i9BysyyQ

1. phi

Here is what I got after running the program sage: load "sqrt.sage" 1 2 3 4 5 6 7 8 9 10 11 12 13 The square root of 25 within a margin of 0.0100000000000000 is 5.00030517578125

2. tcc8m

Phi thank you for the thorough answer but I am still confused. In line 6 there is the variable ans= (high + low)/2 and in line 15 there is the same variable. If you change the low value from 0 to ans = 3.125 because 3.125*3.125 < 25 then doesn't that change the high value to 3.125 as well since both variables point to the same variable ans? I know that 6.25 * 6.25 > 25 which means that high takes the value of ans = 6.25. At this point forward does high actually = 6.25. or does high = ans. I am confused at how the variables high and low map to ans? I appreciate your help!

3. tcc8m

I think I understand now. So in round one high = 25, low = 0. ans = (0 +25)/2 = 12.25. 12.25*12.25 > 25 and so now high = the number 12.25 that ans at one point was equal to. I think I know what you mean. Would this be a good simple example?: If a=9 and b= a a = 10 b = 9 then right?

4. phi

I looked into more details on python, and I am leading you astray a bit See http://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/graphics.html#issues-with-mutable section 2.4.7. More on Mutable and Immutable Types Python does use pointers as you suggest, but it acts as if it does not when dealing with immutable types such as int or str. in python, when you say high= ans both ans and high are pointing to the same number but when you change ans with ans= (high+low)/2, it allocates a new location for ans, and gives this new location the new value.

5. phi

to answer your question If a=9 and b= a at this point, both a and b "point" to a location with 9 in it. a = 10 This causes a to point to a different location with 10 in it b will continue to point to the location with 9 in it b = 9 I think this causes b to point to a new location with 9 in it...

6. phi

So I should revise my previous answer when you ask if both low and high map to same ans... once you compute a new number, python allocates a new memory location, and low (or high) points to the new location. So the "mapping" does not last

7. phi

you asked high takes the value of ans = 6.25. At this point forward does high actually = 6.25. or does high = ans. high = ans. But when you execute the line ans = (high + low)/2.0 ans points to a different location with this new value. so though it is wrong to say high and ans are always different, python makes it look like they are always different variables with their own memory locations.

8. tcc8m

ok that makes it clear. Thank you for helping me through it. I feel much better now. Also, thank you for the link. I also found another good link that explains the concept with pictures. http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables

9. phi

ok, glad I didn't lead you to far off ... I don't know python, so I jumped to conclusions...all these languages have the same underlying ideas, but the details do vary.

Find more explanations on OpenStudy