Project 3: Fixed Point Arithmetic
Due Date:
Project 3 is due at midnight Sunday, March 19, 2000.
Note that this means before 23:59:59 on Sunday evening. The standard
late policy applies.
Objectives
The objectives of this project are:
- To gain more experience writing classes in C++
- To further practice operator overloading
- To learn to implement an overloaded stream output operator
Background
Mathematicians, scientists and engineers have long required real numbers
to perform the calculations required in their respective fields. With
the advent of the computer to do integer operations, real number arithmetic
was sure to follow. This real number arithmetic manifested itself as
floating point arithmetic. The idea is that the number of places
of precision is variable based on the requirements of the calculation
in question. The decimal point "floats", hence the name.
However, floating point arithmetic was originally emulated,
very slow, and computationally intensive. Because of this, many
machines have a coprocessor (a separate, dedicated processor)
for handling these floating point
instructions. In many cases, they can be made to run at the same speed or
even faster than integer calculations, but at the expense of heat and power.
An alternative to floating point arithmetic is fixed point arithmetic.
Fixed point arithmetic sacrifices precision in order to gain back some of the
performance of integer arithmetic. In the distant past, floating point
coprocessors were expensive so applications often relied on fixed point
arithmetic as an inexpensive solution.
Despite the fact that floating point coprocessors are now standard with
consumer desktop machines, the ever shrinking computer is now plagued with
what seemed like lesser concerns before: heat and power consumption. Guess
what? Most personal digital assistants (PDAs -- like 3Com's Palm series) don't
have coprocessors for floating point arithmetic. We're back to emulation
and again we find a home for our hybrid, fixed point arithmetic.
Tasks
You have been contracted by 202Soft, Inc., a leading (pre IPO) campus-wide
developer of platform independent PDA software, to write
a C++ class to handle fixed point arithmetic. This library will allow their
software developers to freely switch between their own floating point emulation
code and your fixed point code.
The crunch is on. With a product release only weeks away, 202Soft urgently
needs a beta release of your library. They've made it abundantly clear that
performance is not an issue at this time. However, they are desperate to
make sure that compatibility with your library is a resolved issue. If your
code is well abstracted and provides an API that integrates seamlessly with
builtin types, their product release can incorporate an optimized version
of your library in a bug fix after the initial release.
You should write a Fixed
class that handles fixed point
arithmetic. The following is an example Fixed.H file.
#ifndef CMSC202_FIXED_H
#define CMSC202_FIXED_H
#include
class Fixed {
public:
// ------------------------------------------------
// A Fixed constructor
// ------------------------------------------------
Fixed(float value=0.00, int precision=2);
// ------------------------------------------------
// Change the precision of the current number.
// This should not change the value stored, only
// it's precision (i.e. 30.00 becomes 30.000 not
// 3.000 with a change of precision from 2 to 3.)
// ------------------------------------------------
void setPrecision(int);
// ------------------------------------------------
// General arithmetic compatibility methods
// ------------------------------------------------
Fixed operator +(const Fixed &) const;
Fixed operator -(const Fixed &) const;
Fixed operator *(const Fixed &) const;
Fixed operator /(const Fixed &) const;
Fixed operator =(const Fixed &);
Fixed operator =(float);
// ------------------------------------------------
// Type cast operators to convert Fixed into
// primitive types
// ------------------------------------------------
operator float() const;
// ------------------------------------------------
// An overloaded output operator for Fixed
// ------------------------------------------------
friend ostream & operator <<(ostream &, const Fixed &);
// ------------------------------------------------
// The below are included for math.h compatibility
// ------------------------------------------------
friend Fixed sin(const Fixed &);
friend Fixed cos(const Fixed &);
friend Fixed atan(const Fixed &);
friend Fixed exp(const Fixed &, int);
private:
int value; // The value stored * 10^precision
int precision;
};
#endif
Your program must implement all of the methods listed above
without changing their signatures. This is the public
interface of the class and other developers are
counting on the fact that has the interface that you see above. You may
add private members and methods as you see fit.
Remember: Your chance at getting in at the pre-IPO price is
on the line here.
Your implementations of the above methods must satisfy the
requirements listed.
Discussion About Fixed Point Arithmetic
There are a few peculiarities with fixed point arithmetic. The following
section outlines a few of them.
- Watch out for overflow. Overflow is what happens when you
try to store a value in a variable that is too large for that
variable's type. Your program should catch overflow errors
and try to correct by:
- Reducing the precision of the result
- Printing a warning message
In the event that the overflow cannot be avoided, a second warning should be
printed and the calculation should be allowed to proceed. At this point,
the results of the operation are undefined. (That means you're not responsible
for what your program evaluates for that expression beyond your warning.)
- Similarly, if a call to
setPrecision
would overflow
the value, print a warning. In this case however, do not change
the value of the Fixed
instance.
- Addition and subtraction of fixed point numbers is fairly
straightforward. If two numbers have the same precision,
check for overflow, add the values and return the result.
For example: 30 and 20, as fixed point values with two places
of precision would be 3000 and 2000. Their sum is 5000, or 50.00.
Subtraction works the same way. How does multiplication differ?
How about division?
- As a rule, when two operands are combined in an expression,
the result's precision should be equal to the maximum precision
of the maximum of the two operands. For example: 1.001 + 2.00
should give 3.001 as a result. Neither operand should be
changed in the process.
- The
math.h
functions listed can use the underlying
math.h
functions in their implementation. The
important part here is that they are present as placeholders
for later, optimized versions of the library routines.
Coding Standards
For this project you must follow the class
coding standards.
Using Code You Didn't Write
Claiming authorship of code that you didn't write is plagiarism and will
be dealt with as academic dishonesty. You should not use code that you
didn't write; it defeats the learning experience of the project. If you do
use outside code on your project, you must site the source. If a
significant portion of the project comes from an outside source, your grade
may be reduced.
Testing Your Program
Remember, your program must compile and run using the CC
compiler on the IRIX machines (umbc8/9). If your program does not compile
or does not generate the correct output, you will not receive full credit
for this project.
You should make sure that you have a makefile named either makefile
or Makefile
and that when you type make
your
program compiles to an executable called proj3
. Failure to
follow these simple directions will result in a reduced grade.
The following sample main will help you test
your program. A sample Makefile has also
been provided.
What to Turn In
You should submit your Makefile
, proj3.C
and any
files needed to build the Fixed
class.
The contents of proj3.C
will not be graded.
This is a placeholder for the "real" test routines that the graders will use.
The graders' proj3.C
will expect the public interface defined
above and will expect to find it in Fixed.H
.
Submitting Your Project
When you have finished and tested your project you can submit it electronically
using submit
. The project name for this assignment is
proj3
. As an example, to submit your Makefile
, you
would type:
submit cs202 proj3 Makefile
More complete documentation for submit
and related commands
can be found here.
Last modified: 5 March 2000