Programming due Saturday, Feb. 15 at 11:59pm, no late submission.
This assignment has four objectives:
- to learn floating path representation and math,
- to write more C code that directly manipulates numbers,
- to appreciate what floating point hardware does, and
- to see if you can follow directions!
About The Assignment
This assignment is designed to teach you how to
do several floating point point operations in C
without using either
the
float
or
double
types. Rather, you will
manipulate the bits of the floating point values using the code in
field.c
that you wrote for PA2.
When you have completed this assignment you will understand how floating point
values are stored in the computer, and how to perform several operations in
the case where the underlying hardware or software does not provide floating
point support. This understanding will be crucial later in the semester
because you will write similar programs for the LC3
computer,
which has no support for floating point.
First read the Getting Started section below and then study the documentation
for flt32.h in the Files tab to understand the details of the
assignment.
Getting Started
Perform the following steps
- Create a directory for this assignment. A general scheme might be
to have a directory for each CS class you are taking and beneath that,
a directory for each assignment. The name of the directory is
arbitrary, but you may find it useful to name it for the assignment
(e.g. PA3).
- Copy five files into this directory. It is easiest to right click on
the link, and do a
Save Target As..
for each of the files.
- Open a terminal and make sure you are in the directory you created
in step 1. The
cd
command can be used for this.
- In the terminal type the following command to build the executable.
make
You should see the following output:
/usr/bin/gcc -g -Wall -c -std=c99 flt32.c
/usr/bin/gcc -g -Wall -c -std=c99 testFlt32.c
/usr/bin/gcc -g -o testFlt32 field.o flt32.o testFlt32.o
- In the terminal type
testFlt32
and read how to run the
the program.
- In the terminal type
testFlt32 bin -3.625
and you should see the output:
dec: -1066926080
hex: 0xC0680000
bin: 1100-0000-0110-1000-0000-0000-0000-0000
What you are seeing it the internal bit pattern of the floating point value
-3.625
expressed as an integer, as hex, and as binary.
You now have a functioning program. All the commands work, however, only bin
will produce correct results at this point.
Completing the Code
Before attempting to write any of the functions of
flt32.c
,
study the documentation in found in the files tab.
Plan what you need to do
before writing code.
The best way to complete the code is to follow a write/compile/test
sequence. Do not attempt to write everything at once. Rather choose one
function and do the following steps.
- Write some or all of one function in
flt32.c
using your
favorite editor.
- Save your changes and recompile
flt32.c
using
make
. You will find it convenient to work with both a
terminal and editor window at the same time.
- Repeat steps 1 and 2 until there are no errors or warnings.
- Test the function you have been working on. Do not attempt to move
on until you complete and thoroughly test a function.
- Repeat steps 1 thru 5 for the remaining functions.
You may work on the functions in any order, but most are very simple and are
support functions for the meat of the code. A sample solution prepared by the
author contained the following:
flt32_get_sign()
- 1 line of code
flt32_get_exp()
- 1 line of code
flt32_get_val()
- 1 line of code
flt32_abs()
- 1 line of code
flt32_sub()
- 1 line of code
flt32_get_all()
- 3 lines of code
flt32_negate()
- 3 lines of code
flt32_left_most_1()
- 10 lines of code
flt32_add()
- 60 lines of code
Your code may be a little longer, but in most every case, these functions
are quite simple. If you find any of your solution is much longer that
stated, you will want to think about how you are approaching the
problem. Remember to take advantage of the functions you wrote that are
documented in
field.h
. Simply call those functions from your code.
Floating Point Addition
The single function
flt32_add()
is the only complex function in
this assignment. Many of the things you need to do can be done by calling the
support functions you have already written and thoroughly tested.
The general algorithm for floating point addition is as follows:
- Extract the sign, exponent, and mantissa for each of the numbers.
- Adjust the number with the smaller exponent so that it has the same
exponent as the other one. This is done by shifting the mantissa
and and incrementing the exponent. When this is complete both numbers
should have the same exponent. This will be the initial value
for the exponent of the sum.
- Convert the sign/magnitude representation to 2's complement.
- Do an integer addition.
- Convert the two's complement back to sign/magnitude
- Normalize the result using shift operations and increment/decrement the
exponent appropriately.
- Reassemble the sign, exponent and value into a 32 bit value.
Checking in Your Code
You will submit the single file
flt32.c
using the Checkin page
on the course webpage.
Specifications
Your program must meet the following specifications:
- Work on your own, as always.
- The name of the source code file must be exactly flt32.c.
- Name the file exactly - upper and lower case matters!
- Comments at the top as shown above.
- Make sure your code runs on machines in the COMCS 120 lab.
- Submit your program to the Checkin tab as you were shown in the recitation.
- Read the syllabus for the late policy.
- We will be checking programs for plagiarism, so please don't copy from anyone else.
Grading Criteria
- 100 points for perfect submission.
- 0 points for no submission, will not compile, submitted class file, etc.
- Each test can make multiple calls to the function being tested, with different values.
- Preliminary Tests
- testCompile: checks that program compiles. (0 points)
- test1: calls testFlt32 with sign to check flt32_get_sign (positive number). (5 points)
- test2: calls testFlt32 with sign to check flt32_get_sign (negative number). (5 points)
- test3: calls testFlt32 with exp to check flt32_get_exp. (5 points)
- test4: calls testFlt32 with val to check flt32_get_val. (5 points)
- test5: calls testFlt32 with abs to check flt32_abs (positive number). (5 points)
- test6: calls testFlt32 with abs to check flt32_abs (negative number). (5 points)
- test7: calls testFlt32 with neg to check flt32_negate (positive number). (5 points)
- test8: calls testFlt32 with neg to check flt32_negate (negative number). (5 points)
- test9: calls testFlt32 with lm1 to check flt32_left_most_1. (5 points)
- test10: calls testFlt32 with add to check flt32_add, (no normalization). (10 points)
- test11: calls testFlt32 with sub to check flt32_sub, (no normalization). (10 points)
- test12: calls testFlt32 with add to check flt32_add, (with normalization). (5 points)
- test13: calls testFlt32 with sub to check flt32_sub, (with normalization). (5 points)
- Final Tests
- Final tests will test other variations of addition and subtraction.
- Final tests will include the preliminary tests.
- These and will be announced at a later time. (25 points)
Submit the single file
flt32.c
to the Checkin tab on the course website, as you
were shown in the recitation, and read the syllabus for the late policy (if necessary).
© 2014 CS270 Colorado State University. All Rights Reserved.