In addition, comparison of double values using operator == is
invariably wrong because of problems with precision. Most often
instead of comparing using x == y, it makes more sense to use
a function that compares numbers using the relative difference:
if ( abs(x -y)/min(x,y) < EPSILON ) numbers are equal
You are to write a class Double (see inheritance hierarchy
below) that can be used as a wrapper, or in place of the built-in type
double. The wrapper class should support all the standard
arithmetic operations, but should use relative difference as the basis
for equality comparison. The wrapper class must also support formatted
I/O using a member function format that takes arguments similar
to those used with the C standard function printf. You
can see the printf man page (or type
man -s3s printf) for details. As an example, to get 4 numbers
after the decimal point you can use:
Double d = 3.1415926535;
cout << d.format("%.4")
Should print 3.1416 -- this is equivalent to using
printf("%.5f") NOT printf("%.5g") which would yield
3.142 (four total places of precision).
You should not do any formatting yourself, simply pass the formatting
argument off using a call of sprintf which prints to an
internal char buffer (C-style string). The return type of
format is string.
Inheritance Hierarchy
In addition to the wrapper Double, you should supply an
abstract base class Number, and wrappers Integer
and Rational. The class Rational should implement
rational numbers (aka fractions) using arbitrarly large integers for
numerator and denominator. For arbitrarily large integers you can use
either the class BigInt (declared in bigint.h, part
of libtapestry) or the gnu class Integer declared in
Integer.h. Rational numbers should be normalized to always be
in lowest terms. You don't need support formatting for the class
Rational.
Test Program
You should turn in a test program or programs showing the functionality
of your wrapper classes. You must also write a program to compare the
performance of the class Double with the built-in type
double. You must devise the tests, time similar code with the
two types, and offer gprof output to augment your README description of
the differences.
Submit
Submit a gzipped tar file or several files as shown below.
submit108 number README Makefile *.cc *.h
Grading
This assignment is worth 20 points awarded as follows.
| Criteria
| Points
|
| Number, Double, Integer |
5 |
| Rational |
5 |
| Style/Coding standards |
5 |
| Test Programs, README, write-up |
5 |
Owen L. Astrachan
Last modified: Tue Feb 11 16:47:55 EST