Reference no: EM13856
Write two short C programs and solve four exam-style problems.
Details on the programs are as follows.
Program : builddbl.c
This program takes four inputs representing the sign, exponent, and integer and fractional parts of the significand of a double. The significand M will look like d.xxxxxxxxxxxxx where each x is a hexadecimal digit. The digit d left of the "decimal point" is the integer part of the M. It is 1 for normalized doubles and 0 for denormalized doubles. It will not appear directly in the double you build, but it will affect the exponent. The thirteen x's represent the fractional part of the double. Your program should use the four parts to construct a double and print it both as a double (using %.17g format) and its 16-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 doubles. Here are some sample outputs to show the behavior of this program:
As with assignment #1 I want you to write both programs with main functions that take care of command line inputs and printing the output. The main funcitons will call auxiliary functions to do the work of assembling or disassembling the double's. These auxiliary functions will use a C structure to hold the parts of adouble. The definition of the structure and the declarations of the auxiliary functions are given in hw2.h. You should include it in your programs with the following statement: #include "hw2.h". Note the change in punctuation. Library headers are written <stdio.h> , while headers from your own directory are written"hw2.h".
Also included in hw2.h are declarations for two functions d2ll and ll2d given below. These functions convert double to long long int and back again without changing the bit pattern. You will use them to convert your double's to long long int's before taking them apart using integer arithmetic. You will build yourdouble's using integer arithmetic to make a long long int with the desired bit pattern. Then you convert it to a double as a last step. If the inputs are invalid and you cannot build the double, your auxiliary function should return NaN (Not a Number) which I have defined in hw2.h.
When your auxiliary functions compile successfully and run correctly on a few test inputs you should submit them to the grading server just as you did with functions h2d and d2h in assignment #1.
Instructions are given below.
> builddbl
Usage: builddbl sign, E, intpart, fracpart (parts of a double)
> builddbl + 1023 1 fffffffffffff
value = 1.7976931348623157e+308 (7fefffffffffffff)
> builddbl - 0 1 0000000010000
value = -1.0000000000145519 (bff0000000010000)
> builddbl + -2 1 e62433b79890d
value = 0.47474747474747475 (3fde62433b79890d)
> builddbl - -1022 0 0000000314159
value = -1.594840446316023e-317 (8000000000314159)
> builddbl - -1022 1 0000000314159
value = -2.220738601020418e-308 (8010000000314159)
> builddbl + 10 2 fffff00000ddd
Usage: sign(+ or -), E(-1022 to +1023), d(0 or 1), f(13 hex digits)
Build failed
> builddbl + -1023 1 0000000000000
Usage: sign(+ or -), E(-1022 to +1023), d(0 or 1), f(13 hex digits)
Build failed
> builddbl + -1021 0 7777777777777
Invalid parts for normalized double
Build failed
> builddbl + 1024 1 0000020000000
Usage: sign(+ or -), E(-1022 to +1023), d(0 or 1), f(13 hex digits)
Build failed
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. If f is given with more than 13 hex digits, you may just use the last 13 digits.
Program #2 : dblparts.c
This program is roughly the inverse of Program #1. It takes a list of doubles and prints out the parts: sign, exponent, and integer and fractional parts of the significand. Here are some sample outputs:
>dblparts
Usage: dblparts f1 f2 f3 ... (list of floating point numbers)
>dblparts .47474747474747475 -1.594840446316023e-317 -1.7976931348623157e308
input 1 = .47474747474747475
sign = +, E = -2 intpart = 1 fracpart = e62433b79890d
input 2 = -1.594840446316023e-317
sign = -, E = -1022 intpart = 0 fracpart = 0000000314159
input 3 = -1.7976931348623157e308
sign = -, E = 1023 intpart = 1 fracpart = fffffffffffff
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 ll2d turns a long long int into a double with the same bit pattern. The function d2ll turns a double into an long long int with the same bit pattern. This is a different result than you get with a cast: ll2d(x) != (double)x . Try to figure out how they work, but that is not part of this homework:
double ll2d(unsigned long long int l)
/* convert 64-bit integer to double with same bit pattern */
{
union {
double dd;
unsigned long long int ll;
} a;
a.ll = l;
return a.dd;
}
unsigned long long int d2ll(double d)
/* convert double to 64-bit integer with same bit pattern */
{
union {
double dd;
unsigned long long int ll;
} a;
a.dd = d;
return a.ll;
}
The problems that go with this assignment are the Piazza Resources page: Problems_2.rtf. Fill in the answers and put it in your drop box.