CompOrg Spring 2004

C Programming and IEEE Floating Point

Due Date: Feb 19th (by 11:00PM)
Submit using WebCT: assignment HW1

Late Penalty: 10% per day. No submissions will be accepted after 11:00PM on Feb 26th

You may not share code with anyone else, you must work alone!
Feel free to discuss the project with anyone, just make sure you don't share code in any form! You are expected to write all the code you submit (do not submit code you find on the WWW!!!).

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.

Generating binary (ASCII encoded) from a float: print_float

void 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).

Generating a C float from ASCII encoded binary: get_float

float 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" );

Sample Test Code

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).

#include <stdio.h>   /* needed for printf */
#include <stdlib.h>  /* needed for atof */
 
void print_float(float f);
float get_float(char *sign, char *e, char *significand);
 
 
int main(int argc,char **argv) {
 
  float x;
 
  /* if there are 3 args on the command line, call get_float.
         if there is just one, convert it to a float and call
         print_float
  */
 
  if (argc==2) {
        /* only one arg - assume it's a float and call print_float */
        /* convert argv[1] to float */
        x = atof(argv[1]);
        print_float(x);
  } else if (argc==4) {
        x = get_float(argv[1],argv[2],argv[3]);
        printf("%f\n",x);
  } else {
        /* invalid command line */
        printf("Invalid command line\n");
  }
  return(0);
}
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: