Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PLEASE MAKE SURE YOUR ANSWER WORKS WITH THIS HEADER FILE BELOW (named fp.h) #def

ID: 3889682 • Letter: P

Question

PLEASE MAKE SURE YOUR ANSWER WORKS WITH THIS HEADER FILE BELOW (named fp.h)

#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) ;

THIS OUTLINE NEEDS TO BE FOLLOWED

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#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;
}

INPUT: Read in a ‘program’ and call your functions to implement these programs.   An example of one possible program might be: x = 18.113 print x y = 4.5 a = x + y print a z = x * y

print z

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

Implement a 15 bit floating point representation, where 6 bits are for the exponent and 8 are for the fraction. Using bit level operators, 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 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. 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, 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 - 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) {}

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

Explanation / Answer

I know the code

#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
/* Define temporary variables */
double value;
int e;
double f;

/* Assign the value we will find the exp of */
value = 1.5;

/* Calculate the fraction and exponential of the value */
f = frexp(value, &e);

/* Display the result of the calculation */
printf("The Fraction and Exponential of %f are %f and %d ", value, f, e);

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote