I am stuck on hand out 3 on an example using bisection search to find the square root of x (x=25).
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?
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga.
Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus.
Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
Here is what I got after running the program
sage: load "sqrt.sage"
The square root of 25 within a margin of 0.0100000000000000 is 5.00030517578125
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!
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?:
and b= a
a = 10
b = 9 then right?
Not the answer you are looking for? Search for more explanations.
I looked into more details on python, and I am leading you astray a bit
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.
to answer your question
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...
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
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.
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.
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.