CS5nm, Fall 2008

ex08: writing and testing simple math functions, part 2

 


Goals for this lab

This lab will give you more practice with

This builds on the skills from ex05

Step by Step Instructions

Step 0: Getting Started

Step 0a: Open IDLE to the usual starting point

See lab01, step 3, and ex05 step 0 for instructions on bringing up IDLE to the "usual starting point" (Python Shell on the left, window for code on the right).

Step 0b: Create a CS5NM/ex06 folder

Find your CS5NM folder—on your Desktop, or in your Documents folder, or on your N:\ drive—wherever you created it, and create a folder inside called ex08. We'll store all your files for this exercise in that folder.

Step 1: Reading some partially finished code

The program at the link below contains five function definitions.

http://www.cs.ucsb.edu/~pconrad/cs5nm/08F/ex/ex08/ex08PhillConrad.py

Before you copy and paste this, save it and run it, just take some time to read over the code.

The first two function defintions are for check_expect and check_expect_within_tolerance.

The check_expect function was already introduced in ex05. If you are not clear on how it works, or what its purpose is, you may want to review ex05 at this moment.

The check_expect_within_tolerance function is new. It is used when we are concerned that the values calculated for our test cases might not be exact, because, for example, they involve quantities like pi, or square roots, or other numbers that have no exact representation in binary. Therefore, we consider the test to be a success (passed) if the calculated answer is very close to our expected answer—that is, within some tolerance.

We use this formula:

math.fabs(check-expect) <= tolerance

This formula says to subtract the expected value from the value we are checking, and then take the absolute value by using the Python built in function math.fabs. As you probably remember from high school math, the absolute value function converts negative numbers to positive ones, and leaves positive numbers unchanged. The f in fabs stands for floating point, meaning that the function can work with numbers like 23.7 not only integers.

The formula tests whether the difference between check and expect is less than or equal to our tolerance. Tolerance will be a number like 0.0001, meaning that if we are within 0.0001 of the correct answer, we'll consider that as "close enough".

As an example, if we calculate something based on pi, and use 3.14159 as our estimate of pi, then we might reasonably expect our answer to be within 0.0001 of the correct answer (depending on the formula, and the magnitude of the radius we are using.)

In this exercise, you'll be asked to add some test cases, fill in some function defintions, and in at least one case, to write your own function definition. We'll actually use some of your results from the first exercise of the course (ex01), the one where you thought about how to calculate gas mileage.

Once you've looked over all of this, you are ready to move on to Step 2.

Step 2: Getting the file to run

This file is actually missing one important line. I left it out on purpose so that you would see what happens when we forget it.

In this file, we are using math.pi and math.fabs. That means that we need to have the line import math somewhere earlier in the file—usually near the top. In this case it is missing, so if you save the file and run it, you'll get an error like this (I'm showing only the last line of the error message)

...
 NameError: global name 'math' is not defined
 >>> 

Try it now (if you haven't already). Save your file under the name ex08YourName.py (e.g. ex08TiffanyAching.py, ex08RufusDrumknott.py), and try running the file. You should see the message listed above.

To fix, this we need to add this line near the top of the file:

import math

What import math does is to pull in the definitions of functions such as math.sqrt(), math.fabs(), math.sin(), and many others. You can see a list of these functions by typing help(math) at the Python shell prompt.

Try adding the import math line—you'll see a comment near the top of the file with @@@ characters in it telling you where to add it. Be sure to remove that comment after you add import math.

If you do so, and run the file, you should get this result:

Test areaOfCircle test 1 failed: expected 3.14159 but I got 3.14159265359
Test areaOfCircle test 2 failed: expected 12.56636 but I got 12.5663706144
Test areaOfCircle test 3 passed (with tolerance 0.0001)
Test areaOfCircle test 4 passed (with tolerance 0.0001)
Test degreesToRadians test 1 failed: expected 3.14159 but I got -1 (tolerance of  0.0001 exceeded)
Test distanceTravelled test case 1 failed: expected 100 but I got -1 (tolerance

Look at this result carefully. You'll see that test1 and test 2 for areaOfCircle() failed because the result was more exact (in terms of an approximation of pi) then the result we were expecting. However, the function is operating properly.

You'll then see that test 3 and test 4 passed, because we were using our new function, check_expect_within_tolerance().

The output continues with tests for degreesToRadians() and distanceTravelled()that both failed, because the functions returned -1 instead of the correct answer.

Now that the file is running, let's proceed with Step 3.

Step 3: Improving the Code

In this step, we'll make several improvements to the code

Step 3a: Fix the header comment

As with all programs that you modify and submit for credit in this course, you should fix the header comment. Include your name, and the date, and also give credit to the original author of the code. (If you are not sure what to do, you can refer back to ex06 Step 2a for an example of how to do this.)

Step 3b: Fix the tests for areaOfCircle()

There are currently four tests in the file for areaOfCircle(). The first two don't work too well, because they don't have a tolerance defined, and the result of this calculation includes an approximation for pi. So, let's remove those, and renumber the labels in the other two tests to be "test 1" and "test 2" instead of "test 3" and "test 4".

Re-save and re-run to be sure that these tests now pass.

Step 3c: Add two test cases for degreesToRadians()

There is currently only one test case for degreesToRadians(). Add two more, using the check_expect_within_tolerance() function.

Note that it is good practice to add the tests cases before we write the body of the function. There are several reasons this is a good idea:

Re-save and re-run to be sure that your two new tests fail.

Step 3d: Fix the degreesToRadians() function

Replace the stub with the correct formula degreesToRadians() and see your test cases pass.

Step 3e: Repeat this process for distanceTravelled()

Repeat this process for the distanceTravelled() function—that is, first add some test cases, watch them fail, and then add the correct code to make them pass.

Once you've done that, you are ready to write your own function based on the problem we did earlier in the course—the gas mileage problem.

Step 4: Add a function to calculate gas mileage

In exercise ex01, I asked you to think about the steps needed to calculate a gas mileage. You came up with some numbers you need to gather, and then a calculation you would do on those numbers.

Now, translate that into Python code.

When you are finished, you'll have a function definition that can compute gas mileage, and at least two test cases. And, you are finished with this exercise!

Step 5: Submitting your work

Step 5a: Go over the grading rubric below (to maximize your grade)

Towards the end of this web page, there is a grading rubric—that is, a chart showing how much each part of the assignment is worth in terms of points. Your TA and your instructor will use this grading rubric to evalulate your work, so to maximize your grade, you may want to look this over and double check that you have done everything required of you.

If you have any questions, consult your TA or instructor before submitting your work to GauchoSpace for grading

Step 5b: Double check that you removed all the comments with @@@ in them.

One thing to particularly watch out for—throughout the file there are comments containing the symbols @@@.

Step 5c: Making a clean run

Step 5d: Saving this to a transcript file

Then, make a transcript file (just like you did in lab01), like this:

While you are in the IDLE window, not the window with your function defintions:

Step 5e: Submit your work on GauchoSpace.

Submit the files ex08YourName.py and ex08.transcript.txt on GauchoSpace as your submission for Exercise ex08.

Then you are finished with this exercise!


Evaluation and Grading (100 pts)

Grading Rubric:

Step Item Points  
2 and 5 Naming and submitting your files correctly 10  
3a Fixing the header comment 5  
3b Fixing the tests for areaOfCircle() 5  
3c Adding two test cases for degreesToRadians() 10  
3d Fixing the degreesToRadians() function 10  
3e Adding test cases for distanceTravelled() 10  
3e Fixing the distanceTravelled() function 10  
4 Header comment for gas mileage function
explaining what the function consumes, and what it produces.
10  
4 At least two test cases for gas mileage function 10  
4 Correct formula for gas mileage function 10  
5c A clean run in your transcript file, showing that all tests pass 5  
5b Removing all the comments with
@@@ in them before submitting
5  

 

Due Date: Wed 10/22/2008 (5pm)

 


Copyright 2008, Phillip T. Conrad, CS Dept, UC Santa Barbara. Permission to copy for non-commercial, non-profit, educational purposes granted, provided appropriate credit is given; all other rights reserved.