Question: C programming Question, Please help!!! INPUT: For this assignment, you will read in a program and call your functions to implement these programs. x =
C programming Question, Please help!!!
INPUT: For this assignment, you will read in a program and call your functions to
implement these programs.
x = 18.113 print x y = 4.5 a=x+y print a z=x*y print z
An example of one possible program might be:
OUTPUT: The output will be the current values of the given variables at the print statements. For the above program, output would be:
x = 18.0625000000 a = 22.5625000000 z = 81.2500000000
Some of this task is already done for you. I will provide a program that reads in the given programs, saves the variable values and calls the functions (described next) that you will be implementing.
You are going to implement a 15 bit floating point representation, where 6 bits are for the exponent and 8 are for the fraction. Using bit level operators, you will write functions (shown below) to help implement the program statements:
Assignment statement (variable = value) your function computeFP() will be called that will take the input floating point value and convert it to our 15 bit
page1image16584 page1image16744
representation, returning this as an integer. This integer will be saved as the
value of the given variable.
int computeFP(float val) { } // input: float value to be represented // output: integer version in our representation Given the number of bits, the rounding you will have to do for this representation is pretty substantial. For this assignment, we are always going to take the easy way and truncate the fraction (i.e. round down). For example, the closest representable value for 18.113 (rounding down) is 18.0625, as can be seen in the program output.
- Print statement (print variable) for this statement, the value of the variable in
our representation will be converted back to a C floating point using your
function and this will be printed.
float getFP(int val) { } // Using the defined representation, compute the // and return the floating point value - Multiply statement for this statement, you are going to take two values in our
representation and use the same technique as described in class to multiply
these values and return the result in our representation.
int multVals(int source1, int source2) {} - Add statement for this statement, you are going to take two values in our
representation and use the technique as described in class to add these values
and return the result in our representation. DO NOT convert them back to float,
add them, then convert to the new representation.
int addVals(int source1, int source2) {}
To make your life a little easier, we are going to make the following assumptions:
No negative numbers. The sign bit can be ignored.
No denormalized (or special) numbers. If the given number is too small to be
represented as a normalized number, you can return 0. If the number is too large, return -1
THE FOLLOWING IS THE PROVIDED CODE:
#include #include #include #include "fp.h"
int computeFP(float val) { // input: float value to be represented // output: integer version in our representation // // Perform this the same way we did in class - // either dividing or multiplying the value by 2 // until it is in the correct range (between 1 and 2). // Your exponent is the number of times this operation // was performed. // Deal with rounding by simply truncating the number. // Check for overflow and underflow - // For underflow, return 0 // For overflow, return -1 return 2; }
float getFP(int val) { // Using the defined representation, compute the floating point // value // For denormalized values (including 0), simply return 0. // For special values, return -1;
return 2.0; }
int multVals(int source1, int source2) { // You must implement this by using the algorithm // described in class: // Add the exponents: E = E1+E2 // multiply the fractional values: M = M1*M2 // if M too large, divide it by 2 and increment E // save the result // Be sure to check for overflow - return -1 in this case // Be sure to check for underflow - return 0 in this case
return 2; }
int addVals(int source1, int source2) { // Do this function last - it is the most difficult! // You must implement this as described in class: // If needed, adjust one of the two number so that // they have the same exponent E // Add the two fractional parts: F1' + F2 = F // (assumes F1' is the adjusted F1) // Adjust the sum F and E so that F is in the correct range // // As described in the handout, you only need to implement this for // positive, normalized numbers // Also, return -1 if the sum overflows return 2; }
//HEADER FILE
fp.h:
//(may not be changed)
#define EXPONENT_BITS 6 #define FRACTION_BITS 8
int computeFP(float val) ;
float getFP(int val);
int addVals(int source1, int source2) ;
int multVals(int source1, int source2) ;
Please note that the code when given input 18.113 for example, must return x = 18.0625000000 - as noted in the spec, cannot round down to 18.0000000000
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
