CS 47 - T. Howell - Spring, 2008
Homework Assignment #2
Note: This page will look best with Internet Explorer. Firefox does not display some of the fonts as intended.
Due Thursday,
February 28, 2008 by 23:59 as an email attachment hw2.zip
(details below).
You are to write two short C programs and solve two exam-style problems. Details on the programs are as follows.
Program #1: buildfloat.c
This program takes three inputs representing the sign, exponent, and significand of a float.
It uses them to construct the float and print it
both as a float (using %.8g format) and its 8-digit hexadecimal representation.
Your program need not deal with infinity or NaN (not a number), but it should deal properly with
both normalized and denormalized floats. Here are some sample outputs to show the behavior of this
program:
> buildfloat
Usage: buildfloat sgn, exp, sig (parts of floating point number)
> buildfloat 1 127 1.fffffe
value = 3.4028235e+38 (7f7fffff)
> buildfloat -1 0 1.000100
value = -1.0000153 (bf800080)
> buildfloat 1 -2 1.e62414
value = 0.474747 (3ef3120a)
> buildfloat 1 -127 0.00dab0
value = 3.9225147e-41 (00006d58)
> buildfloat 1 127 2.000000
Usage: sgn(+1 or -1), exp(-127 to +127), sig(0 to 2-eps)
> buildfloat 1 -127 1.000000
Invalid parts for denormalized float
> buildfloat 1 -126 0.fffffe
Invalid parts for normalized float
As before, your code should always print an error message if a command
line input is not of the proper form or a value is out of the proper range.
Hint: I suggest reading the integer part and fractional part (× 223) of the significand
separately as two integers.
Program #2: floatparts.c
This program is roughly the inverse of Program #1. It takes a list of floats
and prints out the parts: sign, exponent, and significand. Here are some sample outputs:
>floatparts
Usage: floatparts f1 f2 f3 ... (list of floating point numbers)
>floatparts .474747 3.9225147e-41 -3.4028235e+38
input 1 = .474747
sign = +, exponent = -2 significand = 1.e62414
input 2 = 3.9225147e-41
sign = +, exponent = -127 significand = 0.00dab0
input 3 = -3.4028235e+38
sign = -, exponent = 127 significand = 1.fffffe
Try to make your output look as close to these sample outputs as possible.
Here are two functions that you might find useful.
The function l2f
turns an
int into a float with
the same bit pattern.
The function f2l
turns a
float into an int with
the same bit pattern. This is a different result than you get with a cast:
l2f(x) != (float)x .
Try to figure out how they work, but that is not part of this homework:
float l2f(int l)
/* convert long integer to float with same bit pattern */
{
union {
float f;
int ll;
} a;
a.ll = l;
return a.f;
}
int f2l(float f)
/* convert float to long integer with same bit pattern */
{
union {
float ff;
int l;
} a;
a.ff = f;
return a.l;
}
Finally, students looking for an extra challenge (and 50% extra credit for this assignment) may
write analogous programs builddbl.c
and dblparts.c. These build and dissect
doubles
instead of
floats.
You might want to use a 64-bit integer datatype called
long long int
which is supported by gcc. This is significantly more challenging, and extra credit will
only be given for programs that are completely correct. Submit only one pair of programs
(for float or
double but not both).
The two written problems are contained in either file
Homework 2 Problems (.doc) or
Homework 2 Problems (.rtf).
They should be submitted in the form of a MS Word file (.doc) or as a .txt or .rtf file
along with your programs.
Submission
requirements and grading system
As noted above, you are to email me
your homework as a zip file hw2.zip,
which should contain
one problems file
(.doc, .rtf. or .txt), and
two source code files only,
with the exact names as
given above, and with no paths
embedded in the zip file. Your
email must be sent to me by 23:59 on Thursday, February 28.
Your email must
have the following subject line:
CS 47 Section x Homework #2 John Doe
but of course with your own name instead of
"John Doe" and your own section number in place of "x" (1 = 1:30, 2 = 5:30). Please also
preserve all spacing and capitalization
in this subject line.
As with any code you submit, each code file must be
appropriately documented with a header comment containing (as a
minimum) your
name, the class and section number, the homework problem number, and a
brief description of the problem.
I will grade according to three criteria: execution tests (70%), source
code (20%),
and documentation (10%). I believe I have specified these three
programs very precisely above, but if you have any questions about the
behavior of the programs it is your responsibility
to ask me before the submission date.
Failure to observe
any of the submission requirements stated above may
result in a grade of 0 on this homework!
Happy programming!