>

CS 1323 Honors, Fall 1996

Individual Project 3
Cosines
Due Monday, September 23, 4:30pm

What to Hand In (see also: example project solution)

The Function You Must Define

Define a function called "cosine" that delivers an approximation to the cosine of its argument.

The argument of cosine will be a number representing an angle measured in radians. As with most measurements (as opposed to numbers produced by counting things), this number will have a fractional part. So, it will be represented in Haskell by a number in the class Fractional. You may assume a more specific type if you like - Float, for example. In this case, your type specification for cosine would be:

cosine :: Float -> Float

Compute your approximation to cosine using the first fifty terms of the Maclarin series for cosine:

cosine(x) ~= 1 - x^2/2! + x^4/4! - x^6/6! + x^8/8! - x^10/10! + ...
where x^n means x raised to the power n and n! means n factorial (that is, n! = 1*2*3*...*n).

This formula approximates the cosine function to many digits of accuracy throughout the range of values of interest (-10*pi to 10*pi) when all the arithmetic is done precisely. The formula will deliver an accurate result in your computations for small arguments, but will be inaccurate for large ones. Don't worry about the inaccuracy for large arguments, but if you get wrong answers for small arguments, there is something wrong with your definition.

Note: This approximation to cosine is loosely related to the methods most computing systems use to compute cosines. But, those methods are much more ingenious. First, they all alleviate (but do not eliminate) the problem with large arguments by taking advantage of the cyclic nature of the cosine function. Second, the polynomial they use as an approximation is of much lower degree. Our approximation polynomial has 50 terms; theirs computes the cosine to six digits of precision using a polynomial with only four terms (or a seven-term polynomial for sixteen-digit precision). Such compact, high-precision approximations are the remarkable products of the work of numerical analysts, one of the fields of study in the domain of computer science.

Ground Rules