Can someone please explain me the code on Lec 5? What is an epsilon?And what does the 'assert' command does?
I don't know which code you're referring to but here are some explanations: 1) epsilon In computer science, it's a no-no to compare floats this way: if float1 == float2: The reason is that computers express floats in base2, not base 10. Since the number of bits that is used to represent a float is limited, the computer representation of a float may be exact. To better illustrate this, try typing these on IDLE: (repr() is used to get the computer's actual representation of the number) myFloat = 0.1 print myFloat print repr(myFloat) As you can see, 0.1 stored in the variable myFloat is NOT EXACTLY 0.1. It is 0.10000000000000001. There is a difference of 0.00000000000000001 between the expected value of the myFloat and the actual value. This is why if you use myFloat == 0.1, you expect it to be True but unfortunately, it will be False. This is where EPSILON comes in. Epsilon serves as the MAXIMUM DIFFERENCE in between two floats in order for them to be considered equal. In other words, if the difference between two floats is less than epsilon, they are considered CLOSE ENOUGH so you can consider them equal numbers. :) 2) assert(condition) The purpose of the assert command is to test if the condition. If the condition is True, then it continues running the program. If not, it terminates the program. I hope these helps ^^
Thank you very much!This really helped me! The code i was referring to was the square root one btw.
assert statement raises an exception how to handle exceptions is explained later in the course or can be found in the book "how to think like a computer scientist" epsilon is the required precision of the calculation because it is not exact if you're referring to the squareRootBi() function then it's argument can be a positive float (if I remember correctly) and epsilon - the required level of precision. Square roots of negative numbers don't exist. The first guess the middle of the interval (0,float) and then the result is squared and evaluated against epsilon. Based upon the evaluation the value is either returned or the new guess is computed (again the middle of another interval formed depending on the evaluation of the previous guess squared).
Join our real-time social learning platform and learn together with your friends!