CS-220 Spring 2016 Lab 3 Square Roots
Background - Calculating Square Roots
We use a square root to demonstrate the properties of floating point numbers, specifically the concept that a floating point number is an approximation of a number, and that we need to deal with the concepts of tolerances and errors.
We will be calculating a floating point square root using an iterative method which, at every iteration, increases the accuracy of the square root, and decreases the error associated with our current approximation of a square root.
For more information on the various different methods of calculating a square root, see the Wikipedia article, Methods of Computing Square Roots. For this lab, we will use one of the simplest and most effective methods of computing square roots, known as the Babylonian Method
Making some Code
- Make a Lab_3 directory in your home directory.
- Download sqrt.c and Makefile and move these files into your Lab_3 directory.
- Edit sqrt.c, by replacing the comment /* Calculate a new value for root here */ with an actual implementation of the Babylonian approximation method, as described on the Wikipedia page.
- Build and Test your implementation.
- How many iterations of the Babylonian method are required to find the square root of 28 to a 0.01 tolerance?
- Hint... if you get in an endless loop, hit Ctlr-C to interrupt your program.
- What is the smallest tolerance for which you can still caclulate the square root of 28? What happens when you specify a smaller tolerance?
Fixing the Endless Loop problem
- Edit sqrt.c by checking to see if your next guess for the square root is the same as your previous guess. In order to do this, you will have to add a "prev_root" variable declaration, save the value of "root" to "prev_root" before you calculate a new guess, and check your new guess against the previous guess. If they are the same, print an error message, and return your current guess.
- What is the smallest tolerance for which you can calculate the square root of 28 without running into the problem where the new guess is the same guess as the old guess?
- Does the tolerance change for a number much larger than 28? Much smaller than 28?
Extending to larger Precision
- Change your sqrt.c file to work with doubles instead of floats. Compile and test.
- Does this change your results? Does this change the tolerances you can acheive?
- Try finding the square root of a very large number, for instance, 1e15 (which is 1 x 1015). How does this affect the tolerance?
- Try finding the square root of a very small number, for instance, 1e-15 (which is 1 x 10-15). How does this affect the tolerance? Hint: in the printf statement, %f prints up to 6 decimal digits. This may not be precise enough for this case. Try changing to %g instead of %f.
How good is good enough?
Suppose you are given the North/South distance from A to B in variable NS, and the East/West distance from A to B in variable EW. Your goal is to compute the straight line distance from A to B, which can be evaluated using the formula:
dist = sqrt((NS * NS) + (EW * EW), tolerance);
You will provide this distance to your friend, who has a water balloon thrower with perfect accuracy. If NS and EW are specified in meters, and you want to throw a water balloon into a trash barrel with a 1.3 meter radius, would a tolerance of 5.0 be good enough? What about 3.0? What about 1.0? What about 0.5?
Lab Report
Download and edit the following file: lab3_report.txt. Then submit your editted file on Blackboard in the Lab 3 submission area.