Fund. CS II (CS152 2006S)

Homework 6: Square Roots

Assigned: Friday, February 10, 2006
Due: 8:00 a.m., Monday, February 13, 2006

Summary: In this assignment, you will write a utility method to compute square roots. calculator.

Purpose: To give you an opportunity to explore static methods, recursion, and computation with BigDecimal values.

Contents:

The Assignment

As you may have noted, Math.sqrt only accepts values of type double. What happens if we want to compute the square root of a BigDecimal? One possibility is that we could convert the BigDecimal to a double, compute the square root, and then convert back. Unfortunately, that technique essentially eliminates the benefit of BigDecimal values - that they can be more accurate than double values.

The alternative is to write our own procedure. Fortunately, there is a well-known algorithm, the Newton-Raphson method, for computing square roots. The algorithm starts with some approximation of the square root and repeatedly improves that approximation until the approximation is good enough (less than some specified epsilon).

Suppose we are computing the square root of a number n, and our current approximation is a. How do we improve a? We take the average of a and n/a. Note that this is a very sensible way to improve approximations because (a) if the approximation is already correct, a and n/a will be the same, so the approximation will remain correct; (b) if a is too small, n/a will be too large, so the average will be closer to the root; (c) similarly, if a is too large, n/a will be too small.

What approximation should we start with? 1.0 seems to be a decent starting point.

a. Write and test MyMath.sqrt(BigDecimal d, BigDecimal epsilon).

b. Write and test a variant of the same method that takes a PrintWriter as a third parameter and prints the approximation at every step.

Hints

Unfortunately, division with BigDecimal values is less straightforward than it should be. You may need to take some time to read over (and understand) the different forms of division.

Submitting Your Work

When you are satisfied with your work, you should email me the method you have written (use cut and paste to extract it from your MyMath class).

Questions

When you ask questions, I'll try to put the answers here.

 

History

Thursday, 9 February 2006 [Samuel A. Rebelsky]

 

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Tue May 9 08:31:08 2006.
The source to the document was last modified on Mon Feb 13 09:45:10 2006.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS152/2006S/Homework/hw.06.html.

You may wish to validate this document's HTML ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu