Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 8 Online
OpenStudy (anonymous):

I was watching lecture 4 in the spring 2011 series and I am a little confused about the findRoot function in the handout. Specifically, in the findRoot function, the line: if isEven(pwr) and val < 0: If I understand this line correctly, the code returns a None value if the value is negative and the pwr is even, so you can calculate the cube root of a negative number for example, but not the square root or fourth root. Is there a reason for this limitation, or am I missing something Thanks

OpenStudy (anonymous):

This is the code: def isEven(i): """assumes i a positive int returns True if i is even, otherwise False""" return i%2 == 0 def findRoot(pwr, val, epsilon): """assumes pwr an int; val, epsilon floats pwr and epsilon > 0 if it exists, returns a value within epsilon of val**pwr otherwise returns None""" assert type(pwr) == int and type(val) == float\ and type(epsilon) == float assert pwr > 0 and epsilon > 0 if isEven(pwr) and val < 0: return None low = -abs(val) high = max(abs(val), 1.0) ans = (high + low)/2.0 while not withinEpsilon(ans**pwr, val, epsilon): print ('ans =', ans, 'low =', low, 'high =', high) if ans**pwr < val: low = ans else: high = ans ans = (high + low)/2.0 return ans

OpenStudy (anonymous):

sorry, you also need this to make it all work: def withinEpsilon(x, y, epsilon): """x,y,epsilon ints or floats. epsilon > 0.0 returns True if x is within epsilon of y""" return abs(x - y) <= epsilon

OpenStudy (e.mccormick):

The function is operating in the real numbers. Therefore, it does not attempt to calculate complex conjugates.

OpenStudy (e.mccormick):

\(\sqrt{-1}=i\) and \(\sqrt{9}=\pm 3\) \(\sqrt{-9}\implies \sqrt{9}\sqrt{-1}=\pm 3i\)

OpenStudy (e.mccormick):

Also, when you paste code blocks as basic text, they can not be properly copied. They collapse into one line when pasted. If you put ``` on a separate line above and below the code, it changes it into a preformatted block. You can also use a code pasting service to get a similar effect, which is better for longer bits of code. For example, try copying and pasting these: def withinEpsilon(x, y, epsilon): """x,y,epsilon ints or floats. epsilon > 0.0 returns True if x is within epsilon of y""" return abs(x - y) <= epsilon ``` def withinEpsilon(x, y, epsilon): """x,y,epsilon ints or floats. epsilon > 0.0 returns True if x is within epsilon of y""" return abs(x - y) <= epsilon ``` And here are some free paste services: http://gist.github.com/ http://pastebin.com/ http://dpaste.com/ Dpaste is the fastest to use, but not intended for long term items.

OpenStudy (anonymous):

Funny how working through this course has forced me to brush up on my math skills while learning programming skills! Thanks

OpenStudy (e.mccormick):

Well, computers don't speak English. They speak math. \(\ddot \smile\)

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!