-3
def findRoot1(x, power, epsilon):
    low = 0
    high = x
    ans = (high+low)/2.0
    while abs(ans**power - x) > epsilon:
        if ans**power < x:
             low = ans
        else:
             high = ans
        ans = (high+low)/2.0
    return ans



def findRoot2(x, power, epsilon):
    if x < 0 and power % 2 == 0:
        return None
#can't find even powered root of negative number
    low = min(0, x)
    high = max(0, x)
    ans = (high+low)/2.0
    while abs(ans**power-x) > epsilon:
        if ans**power < x :
            low = ans
        else:
            high = ans
        ans = (high+low)/2.0
    return ans


def findRoot3(x, power, epsilon):
    """x and epsilon int or float, power an int
          epsilon > 0 and power >= 1
          returns a float y s.t. y**power is within epsilon of x.
          if such a float does not exist, it returns None."""
    if x < 0 and power % 2 == 0:
        return None
#can't find even powered root of negative number
    low = min(-1, x)
    high = max(1, x)
    ans = (high+low)/2.0
    while abs(ans**power-x) > epsilon:
        if ans**power < x :
            low = ans
        else:
            high = ans
        ans = (high+low)/2.0
    return ans

Why does findRoot1(-27.0, 3, 0.001) fail in the first case? How is the logic made?

Why does findRoot2(0.25, 3, 0.001) fail in the second case? How findRoot2(-27.0, 3, 0.001) pass here?

It works everything for the third case. How?

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
Eliza
  • 101
  • 1
  • 1
  • 9

1 Answers1

1

The issues in the cases are -

  1. First Case : You are assuming that the input you get x would always be positive , since you are always setting it to high, so when sending a negative number, ans in first iteration is -13.5 and since (-13.5)**3 is negative, it is always less than epsilon, hence you set -13.5 to low and from there on it keeps decreasing (goes to -20.25 in the next iteration) till it reaches -27 (that is when low and high both become -27) and then it goes into infinite loop.

  2. Second Case : You are not handling the case where the number is less than 1 , in such case, power of that number would be lesser , for example , x = 0.125 , x^3 = 0.001953125 . But your logic for second case depends on ans**power to be always greater than x , which would only work when x itself is greater than 1. Again, this causes low to be set to 0.125 after first iteration, and then it keeps on getting increased untill low becomes equal to high = 0.25 , in which case it enters infinite loop.

  3. Third Case : It works, because you changed the conditions of setting low and high such that ans is not less than 1 and it handles negative numbers as well.

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
  • In the first case, how does it go to -20.25 in the next iteration? how does low and high become -27? – Eliza Jun 20 '15 at 12:25
  • 1
    first iteration `ans = -13.5` and `high = -27` , `ans**3` is also negative and lower than -27 (not higher , hope you undertand that) , and hence you set low = -13.5 (according to your logic) , now computing ans again, `(low+high)/2` , it becomes `-20.25` – Anand S Kumar Jun 20 '15 at 12:27
  • I don't get the second case. – Eliza Jun 20 '15 at 12:27
  • what don't you get in the second case – Anand S Kumar Jun 20 '15 at 12:28
  • 1
    **second case** - first iteration , `high` = `0.25` , `low` = `0` , `ans = 0.125` , `ans**power` = `0.0019...` , according to your condition, you set low = something, if `ans**power` is less than x , x in first iteration is 0.25 and `ans**power` is `0.0019..` , hence you set low to 0.125 , and then computing ans again you get `0.1875` and this keeps on happenning till both reach same value – Anand S Kumar Jun 20 '15 at 12:31
  • how do we set our mind into the logical thinking like this while writing such codes? – Eliza Jun 20 '15 at 12:34
  • 1
    What I do is visualize the data changing in each step, when reading or writing the code, but its different for all. – Anand S Kumar Jun 20 '15 at 12:35
  • well m a newbie in programming n python is my first choice..so can u be my mentor? – Eliza Jun 20 '15 at 12:40
  • Not sure if I am qualified to be anyone's mentor, but if you have any programming questions you can surely ask, If I know i will try to help – Anand S Kumar Jun 20 '15 at 12:43
  • I think you are qualified to be a mentor for me at least. So, my email id is: elizakarki13@gmail.com we can talk on hangout>? – Eliza Jun 20 '15 at 12:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/81072/discussion-between-anand-s-kumar-and-eliza). – Anand S Kumar Jun 20 '15 at 12:49