DEBUG the following C code. It is not doing exactly what the instructions below
ID: 3891060 • Letter: D
Question
DEBUG the following C code. It is not doing exactly what the instructions below need it to. HIGHLIGHT what you have changed and explain why.
// C code
// fp.c file
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "fp.h"
// Create method named "computeFP" to compute the floting point
Int computeFP(float val) {
// if zero just return here were done.
if (val == 0)
{
return 0;
}
int e = 0;
int retVal = 0;
// calculate the exponent.
if (val >= 2)
{
// if the value is greater than two justify
while (val >= 2)
{
// justify by dividing
val = val / 2;
++e;
}
}
else if (val < 1)
{
// if the val is less than one
while (val < 1)
{
// justify by multiplication
val = val * 2;
++e;
}
e *= -1;
}
val -= 1;
int biasedExponent = e + 15;
int mantissa = 0;
// get the mantissa.
float sevenBits = val * (pow(2, 7));
mantissa = (int)sevenBits;
// Create an "if" statement to check biased expnents is largest number or not
if (biasedExponent > 30)
{
return -1;
}
if (biasedExponent < 1)
{
return 0;
}
// pack the number
retVal |= biasedExponent;
retVal <<= 7;
retVal |= mantissa;
return retVal;
}
float getFP(int val) {
float fraction = ((val & 0x7F) / (float)pow(2, 7)) + 1;
int exponent = ((val >> 7) & 0x1F);
// denormalized or zero
if (exponent == 0)
{
return 0;
}
// special number
if (exponent == 0x1F)
{
return -1;
}
// get exponent
int e = exponent - 15;
// check to see if negative
if (e < 0)
{
int i;
for (i = e; i < 0; ++i)
{
// normalize
fraction = fraction / 2;
}
}
else
{
int i;
for (i = 0; i < e; ++i)
{
fraction = fraction * 2;
}
}
return fraction;
}
int
multVals(int source1, int source2) {
int exponent1 = ((source1 >> 7) & 0x1F);
int exponent2 = ((source2 >> 7) & 0x1F);
int e1 = exponent1 - 15;
int e2 = exponent2 - 15;
int e3 = e1 + e2;
float frac1 = ((float) (source1 & 0x7f));
float frac2 = ((float) (source2 & 0x7f));
// + one since that is the point to the left
frac1 = (frac1 / ((float)pow(2, 7))) + 1;
frac2 = (frac2 / ((float)pow(2, 7))) + 1;
// check for overflow
if ((e3 + 15) > 30)
{
return -1;
}
if ((e3 + 15) < 1)
{
return 0;
}
// is the multiplication of the mantissa
float frac = frac1 * frac2;
// if bigger than two must rejustify
if (frac >= 2)
{
frac /= 2;
++e3;
}
// rebuild the biased exponent
int new_exp = 15 + e3;
// get rid of the leading point since it is assumed
frac -= 1;
frac *= (float)pow(2, 7);
// truncate the float
int newFrac = (int) frac;
// build the new number
int retVal = 0;
retVal |= new_exp;
retVal <<= 7;
retVal |= newFrac;
return retVal;;
}
int
addVals(int source1, int source2) {
int exponent1 = ((source1 >> 7) & 0x1F);
int exponent2 = ((source2 >> 7) & 0x1F);
int e1 = exponent1 - 15;
int e2 = exponent2 - 15;
float frac1 = ((float) (source1 & 0x7f));
float frac2 = ((float) (source2 & 0x7f));
// the fractions with the leading 1
frac1 = (frac1 / ((float)pow(2, 7))) + 1;
frac2 = (frac2 / ((float)pow(2, 7))) + 1;
// check to see which exponent is better
if (e1 > e2)
{
int i;
int delta = e1 - e2;
for (i = 0; i < delta; ++i)
{
// justify frac 2
frac2 = frac2 / 2;
++e2;
}
}
else if (e1 < e2)
{
int i;
int delta = e2 - e1;
for ( i = 0; i < delta; ++i)
{
// justify frac1
frac1 = frac1 / 2;
++e1;
}
}
// add the mantissas
float frac = frac2 + frac1;
// if the fraction is greater than 2 justify frac down
// and increment the exponent.
while (frac >= 2)
{
frac = frac / 2;
++e1;
}
// if the fraction is less than 1 justify up
// decrement the exponent.
while (frac < 1)
{
frac = frac * 2;
--e1;
}
// check for overflow
if ((e1 + 15) > 30)
{
return -1;
}
// build the new number
int new_exp = (15 + e1) * (pow(2, 7));
frac -= 1;
frac *= (float)pow(2, 7);
int newFrac = (int) frac;
return new_exp + newFrac;
}
int main()
{
int a = computeFP(18.113);
int b = computeFP(4.5);
printf("%d ", a);
float g = getFP(a);
float h = getFP(b);
printf("First %f : second %f ", g, h);
int mult = multVals(a, b);
float f = getFP(mult);
printf("Multiplication %f ", f);
int add = addVals(a, b);
float q = getFP(add);
printf("Addition %d ", add);
printf("Addition %f ", q);
}
NPUT: 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
Below is the code: -
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "fp.h"
// Create method named "computeFP" to compute the floting point
int computeFP(float val) {
// if zero just return here were done.
if (val == 0)
{
return 0;
}
int e = 0;
int retVal = 0;
// calculate the exponent.
if (val >= 2)
{
// if the value is greater than two justify
while (val >= 2)
{
// justify by dividing
val = val / 2;
++e;
}
}
else if (val < 1)
{
// if the val is less than one
while (val < 1)
{
// justify by multiplication
val = val * 2;
++e;
}
e *= -1;
}
val -= 1;
int biasedExponent = e + 15;
int mantissa = 0;
// get the mantissa.
float sevenBits = val * (pow(2, 7));
mantissa = (int)sevenBits;
// Create an "if" statement to check biased expnents is largest number or not
if (biasedExponent > 30)
{
return -1;
}
if (biasedExponent < 1)
{
return 0;
}
// pack the number
retVal |= biasedExponent;
retVal <<= 7;
retVal |= mantissa;
return retVal;
}
float getFP(int val) {
float fraction = ((val & 0x7F) / (float)pow(2, 7)) + 1;
int exponent = ((val >> 7) & 0x1F);
// denormalized or zero
if (exponent == 0)
{
return 0;
}
// special number
if (exponent == 0x1F)
{
return -1;
}
// get exponent
int e = exponent - 15;
// check to see if negative
if (e < 0)
{
int i;
for (i = e; i < 0; ++i)
{
// normalize
fraction = fraction / 2;
}
}
else
{
int i;
for (i = 0; i < e; ++i)
{
fraction = fraction * 2;
}
}
return fraction;
}
int multVals(int source1, int source2) {
int exponent1 = ((source1 >> 7) & 0x1F);
int exponent2 = ((source2 >> 7) & 0x1F);
int e1 = exponent1 - 15;
int e2 = exponent2 - 15;
int e3 = e1 + e2;
float frac1 = ((float) (source1 & 0x7f));
float frac2 = ((float) (source2 & 0x7f));
// + one since that is the point to the left
frac1 = (frac1 / ((float)pow(2, 7))) + 1;
frac2 = (frac2 / ((float)pow(2, 7))) + 1;
// check for overflow
if ((e3 + 15) > 30)
{
return -1;
}
if ((e3 + 15) < 1)
{
return 0;
}
// is the multiplication of the mantissa
float frac = frac1 * frac2;
// if bigger than two must rejustify
if (frac >= 2)
{
frac /= 2;
++e3;
}
// rebuild the biased exponent
int new_exp = 15 + e3;
// get rid of the leading point since it is assumed
frac -= 1;
frac *= (float)pow(2, 7);
// truncate the float
int newFrac = (int) frac;
// build the new number
int retVal = 0;
retVal |= new_exp;
retVal <<= 7;
retVal |= newFrac;
return retVal;;
}
int addVals(int source1, int source2) {
int exponent1 = ((source1 >> 7) & 0x1F);
int exponent2 = ((source2 >> 7) & 0x1F);
int e1 = exponent1 - 15;
int e2 = exponent2 - 15;
float frac1 = ((float) (source1 & 0x7f));
float frac2 = ((float) (source2 & 0x7f));
// the fractions with the leading 1
frac1 = (frac1 / ((float)pow(2, 7))) + 1;
frac2 = (frac2 / ((float)pow(2, 7))) + 1;
// check to see which exponent is better
if (e1 > e2)
{
int i;
int delta = e1 - e2;
for (i = 0; i < delta; ++i)
{
// justify frac 2
frac2 = frac2 / 2;
++e2;
}
}
else if (e1 < e2)
{
int i;
int delta = e2 - e1;
for ( i = 0; i < delta; ++i)
{
// justify frac1
frac1 = frac1 / 2;
++e1;
}
}
// add the mantissas
float frac = frac2 + frac1;
// if the fraction is greater than 2 justify frac down
// and increment the exponent.
while (frac >= 2)
{
frac = frac / 2;
++e1;
}
// if the fraction is less than 1 justify up
// decrement the exponent.
while (frac < 1)
{
frac = frac * 2;
--e1;
}
// check for overflow
if ((e1 + 15) > 30)
{
return -1;
}
// build the new number
int new_exp = (15 + e1) * (pow(2, 7));
frac -= 1;
frac *= (float)pow(2, 7);
int newFrac = (int) frac;
return new_exp + newFrac;
}
int main()
{
int a = computeFP(18.113);
int b = computeFP(4.5);
printf("%d ", a);
float g = getFP(a);
float h = getFP(b);
printf("First %f : second %f ", g, h);
int mult = multVals(a, b);
float f = getFP(mult);
printf("Multiplication %f ", f);
int add = addVals(a, b);
float q = getFP(add);
printf("Addition %d ", add);
printf("Addition %f ", q);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.