| CompOrg Spring 2004 |
|   CompOrg Home   |   Assignment   |   Examples   |   How to submit   |   Grading   |   Hints   |   HW1 FAQ |
| Assignment |
This assignment involves the creation of C functions which can be compiled under Linux. We will test your code using gcc as the compiler.
You are to write C functions that deal with IEEE 32 bit floating point numbers. One function must be capable of examining the bits that make up an IEEE 32 bit floating point value and printing out the three fields (sign, significand and exponent) individually. Your program must also be able to construct an IEEE 32 bit floating point number given and ASCII representation of the binary encoding of each field. See the function prototypes and examples below...
Your function definitions will be linked with our main
code, so you just need to write the code for the two functions listed
below. Feel free to write other functions as well, the only
requirement is that you include the functions listed below.
print_floatvoid print_float(float x);
You need to write the function print_float that
generates output to stdout that displays the binary representation of
the sign, exponent and significand for an IEEE 32 bit floating point
value x. For example, if print_float(-7.25) is called, it
should print out the following:
1 10000001 11010000000000000000000
The sign is 1, the exponent is 10000001 (which is how the exponent 2 is represented) and the significand is the representation of 1.1101
Your function must work with any floating point value x (this could include special cases like denorms, infinity, NaNs., etc.). Your best strategy is to look at the actual bits that make up the representation of x (use logical operators to extract individual bits/fields), and simply convert these to something you can print (like the characters '1' and '0'). If you try to derive the individual bits using mathematical expressions it will be difficult to support special cases (denorms, infinity, etc).
get_floatfloat get_float(char *sign, char *exponent, char *significand);
get_float is passed 3 parameters, each is a pointer to
a null terminated ASCII string composed of only the characters '1' and
'0'. The string pointed to by sign will have length 1,
the exponent string will be 8 characters long and significand will be
23 characters long. Your job is to construct the corresponding float
and return it.
For example, the following call should return -7.25 as a float:
f = get_float("1", "10000001", "11010000000000000000000" );
We will be using our own main to test your functions,
so you should not include a main in the C file you
submit. Below is a sample main that you can use for
testing, this code can test each of your functions (by getting parameter
value from the command line).
|
| Examples |
A few examples of how to use the above main are shown
below, '>' is the shell prompt.
> ./hw1 0 0 00000000 00000000000000000000000 > ./hw1 3.141593 0 10000000 10010010000111111011100 > ./hw1 -123.456 1 10000101 11101101110100101111001 > ./hw1 0 00000000 00000000000000000000000 0.000000 > ./hw1 0 10000000 10010010000111111011100 3.141593 > ./hw1 1 10000101 11101101110100101111001 -123.456001 > |
Notice that the value for -123.456 is not represented exactly using an IEEE 32 bit floating point number (there is nothing wrong here, it's just a limitation of the representation of floating point numbers)!
| How to submit |
Submission of your homework is done using WebCT (webct.rpi.edu). Once you log in to WebCT and access the CompOrg site, you should click on assignments, then select HW1. Upload your hw1.c file to webct. Make sure your browser is supported before you attempt to upload a file (there is a link to "Check Browser"). Dave will demonstrate submission using WebCT in class.
For this assignment, the file hw1.c is all you need to submit. For future assignments may need to submit multiple files...
Don't send compiled code, only send your C program!
Multiple Submissions: You can resubmit as many times as you want, WebCTwill make sure we get the last file you submit.
| Grading |
Grades will be determined by testing your functions with various
parameter values. We will not test get_float with
anything other that ASCII encoded binary strings (your code can assume
that you are passed strings that contain the characters '0' and '1'
only, and are the appropriate length). We will test your functions
with special case numbers including denorms, infinity and NaN.
Each function is worth 50% of the grade.
| HINTS: |
The simplest approach is to simply manipulate bits (and groups of bits), rather than trying to use arithmetic. If you insist on using arithmetic you will probably not pass all the tests we will give your functions (unless you cover every special case in the IEEE standard).
You should assume that your code is to be run on a machine that uses a small-endian byte ordering. If you do something like this:
float f; char *s = (char *)&f;
You must remember that s[0] is not the MS byte in the IEEE number, (s[3] would actually hold the sign bit and first 7 bits of the representation of the exponent).