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

I\'ve provided parts where I don\'t understand what\'s going on and need answere

ID: 3869792 • Letter: I

Question

I've provided parts where I don't understand what's going on and need answered.

// 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; <----Why multiply by -1?

}

val -= 1; <-----Why subtract 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 <-----------I don't understand it means by "pack the number" and what it does below.

retVal |= biasedExponent;

retVal <<= 7;

retVal |= mantissa;

return retVal;

}

float getFP(int val) {

float fraction = ((val & 0x7F) / (float)pow(2, 7)) + 1; <------ I don't understand why it has to be divided?

int exponent = ((val >> 7) & 0x1F); <-------I don't understand this part at all either

// denormalized or zero

if (exponent == 0)

{

    return 0;

}

// special number

if (exponent == 0x1F)

{

    return -1;

}

// get exponent

int e = exponent - 15;<-------I don't understand this part at all either

// check to see if negative

if (e < 0)

{

    int i;

    for (i = e; i < 0; ++i)

    {

      // normalize <-------I don't understand this part at all either

      fraction = fraction / 2;

    }

}

else

{

    int i;

    for (i = 0; i < e; ++i)

    {

      fraction = fraction * 2; <-------I don't understand this part at all either

    }

}

return fraction;

}

int

multVals(int source1, int source2) {

int exponent1 = ((source1 >> 7) & 0x1F); <-------I don't understand this part at all either

int exponent2 = ((source2 >> 7) & 0x1F);

int e1 = exponent1 - 15; <-------I don't understand this part at all either

int e2 = exponent2 - 15;

int e3 = e1 + e2;

float frac1 = ((float) (source1 & 0x7f)); <-------I don't understand this part at all either

float frac2 = ((float) (source2 & 0x7f));

// + one since that is the point to the left

frac1 = (frac1 / ((float)pow(2, 7))) + 1; <-------I don't understand this part at all either

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 <-------I don't understand why it needs to rejustify and how it does it.

if (frac >= 2)

{

    frac /= 2;<-------I don't understand this part at all either

    ++e3;

}

// rebuild the biased exponent <-------What does it mean by rebuild biased exponent?

int new_exp = 15 + e3;

// get rid of the leading point since it is assumed

frac -= 1;

frac *= (float)pow(2, 7); <-------I don't understand this part at all either

// truncate the float

int newFrac = (int) frac;

// build the new number

int retVal = 0; <-------I don't understand this part at all either

retVal |= new_exp; <-------I don't understand this part at all either

retVal <<= 7; <-------I don't understand this part at all either

retVal |= newFrac; <-------I don't understand this part at all either

return retVal;;

}

int

addVals(int source1, int source2) {

int exponent1 = ((source1 >> 7) & 0x1F); <-------I don't understand this part at all either

int exponent2 = ((source2 >> 7) & 0x1F); <-------I don't understand this part at all either

int e1 = exponent1 - 15;<-------I don't understand this part at all either

int e2 = exponent2 - 15;<-------I don't understand this part at all either

float frac1 = ((float) (source1 & 0x7f));<-------I don't understand this part at all either

float frac2 = ((float) (source2 & 0x7f));<-------I don't understand this part at all either

// the fractions with the leading 1 <-------Why is this special?

frac1 = (frac1 / ((float)pow(2, 7))) + 1; <-------I don't understand this part at all either

frac2 = (frac2 / ((float)pow(2, 7))) + 1;

// check to see which exponent is better <-------I don't understand this part at all either

if (e1 > e2)

{

    int i;

    int delta = e1 - e2; <-------What's delta?

    for (i = 0; i < delta; ++i)<-------I don't understand this part at all either

    {

      // justify frac 2<-------I don't understand this part at all either

      frac2 = frac2 / 2;

      ++e2;

    }

}

else if (e1 < e2)<-------I don't understand this part at all either

{

    int i;

    int delta = e2 - e1;

    for ( i = 0; i < delta; ++i)

    {

      // justify frac1<-------I don't understand this part at all either

      frac1 = frac1 / 2;

      ++e1;

    }

}

// add the mantissas

float frac = frac2 + frac1;

// if the fraction is greater than 2 justify frac down<-------I don't understand this part at all either

// and increment the exponent. <-------I don't understand this part at all either

while (frac >= 2)<-------I don't understand this part at all either

{

    frac = frac / 2;<-------I don't understand this part at all either

    ++e1;

}

// if the fraction is less than 1 justify up<-------I don't understand this part at all either

// decrement the exponent.<-------I don't understand this part at all either

while (frac < 1)<-------I don't understand this part at all either

{

    frac = frac * 2;<-------I don't understand this part at all either

    --e1;<-------I don't understand this part at all either

}

// 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

I've provided parts where I don't understand what's going on and need answered.

// 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 (val == 0) return 0;
   int e = 0;
   int retVal = 0;
   if (val >= 2)
     {
       while (val >= 2)
        {
         // justify by dividing
         val = val / 2;
         ++e;
        }
     }

   else if (val < 1)
    {
      while (val < 1)
       {
        val = val * 2;
        ++e;
       }
      e *= -1; <----Why multiply by -1? [Ans01: because for number less than 1, Exponent would be negative. But in this implimentation instead of --e; the programmer have written ++e. So he multiplies the e by -1 to make negative.]
    }

   val -= 1; <-----Why subtract 1?[Ans02: Because in floatingpoint representation leading one is ignored (this is because of our construction of exponent, leading digit eould always be one so this has no information)]
   int biasedExponent = e + 15;
   int mantissa = 0;

   float sevenBits = val * (pow(2, 7));
   mantissa = (int)sevenBits;
   if (biasedExponent > 30) return -1;
   if (biasedExponent < 1) return 0;

// pack the number <-----------I don't understand it means by "pack the number" and what it does below.[Ans03: You know that in this code we are representing a floating point number as integer. It need to have two parts 1) exponent and 2) mantissa. To put exponent at leading position following two lines are written that first copies the exponent in the integer container called retVal and then shifts all the contents 7 bits in higher side to make room for mantissa. Thisr statement is copying montessa to the retVal ]
   retVal |= biasedExponent;
   retVal <<= 7;
   retVal |= mantissa;
   return retVal;
}

float getFP(int val) {
    float fraction = ((val & 0x7F) / (float)pow(2, 7)) + 1; <------ I don't understand why it has to be divided?[Ans04: by (val & 0x7F) you get the fractional part by now there is no fraction it looks like an integer do to put a decomal infront of the number we divide it by pow(2, 7)]
    int exponent = ((val >> 7) & 0x1F); <-------I don't understand this part at all either [Ans05: Remember while constructing this number we have shifted the bits to seven positions (see Ans03) so it is essential to undo that shifting by (val >> 7). subsequent & 0x1F is essentially neglecting any thing beyond needed area]
    if (exponent == 0) return 0;
    if (exponent == 0x1F) return -1;

    int e = exponent - 15;<-------I don't understand this part at all either[Ans06: what we got was a biased exonent see the code above where "int biasedExponent = e + 15;" so to undo that effect we subtract]
    if (e < 0)
        { int i;
          for (i = e; i < 0; ++i)
              { // normalize <-------I don't understand this part at all either [Ans07: remember "val = val * 2;" this step is to nutralize that work]
                 fraction = fraction / 2;
              }
        }
     else
        {
          int i;
          for (i = 0; i < e; ++i) fraction = fraction * 2; <-------I don't understand this part at all either [Ans08: remember "val = val 2;" this step is to nutralize that work]
        }

     return fraction;
}

int multVals(int source1, int source2) {
    int exponent1 = ((source1 >> 7) & 0x1F); <-------I don't understand this part at all either [refer Ans05]
    int exponent2 = ((source2 >> 7) & 0x1F);
    int e1 = exponent1 - 15; <-------I don't understand this part at all either [refer Ans06]
    int e2 = exponent2 - 15;
    int e3 = e1 + e2;
    float frac1 = ((float) (source1 & 0x7f)); <-------I don't understand this part at all either [refer Ans05]
    float frac2 = ((float) (source2 & 0x7f));
    frac1 = (frac1 / ((float)pow(2, 7))) + 1; <-------I don't understand this part at all either [refer Ans04]
    frac2 = (frac2 / ((float)pow(2, 7))) + 1;
    if ((e3 + 15) > 30) return -1;
    if ((e3 + 15) < 1) return 0;
  
    float frac = frac1 * frac2; // if bigger than two must rejustify <-------I don't understand why it needs to rejustify and how it does it.[Ans09: fractions can be added when exponents are same]
    if (frac >= 2)
       {
         frac /= 2;<-------I don't understand this part at all either [Ans10: if fractions exceed from 1, then it need to be divided. Because we assume digit before decomal is always 1]
         ++e3;
       }

     // rebuild the biased exponent <-------What does it mean by rebuild biased exponent? [Ans11: Biasing is not new here it is also there in function "computeFP" because of standard procedure]
     int new_exp = 15 + e3;

     // get rid of the leading point since it is assumed
     frac -= 1;
     frac *= (float)pow(2, 7); <-------I don't understand this part at all either [Ans12: It is opposit to divide in Ans04]
     int newFrac = (int) frac;

     int retVal = 0; <-------I don't understand this part at all either [refer Ans03. The same logic goes here]
     retVal |= new_exp; <-------I don't understand this part at all either [refer Ans03. The same logic goes here]
     retVal <<= 7; <-------I don't understand this part at all either [refer Ans03. The same logic goes here]
     retVal |= newFrac; <-------I don't understand this part at all either [refer Ans03. The same logic goes here]
     return retVal;;
}

int addVals(int source1, int source2) {
     int exponent1 = ((source1 >> 7) & 0x1F); <-------I don't understand this part at all either [refer Ans05. The same logic goes here]
     int exponent2 = ((source2 >> 7) & 0x1F); <-------I don't understand this part at all either [refer Ans05. The same logic goes here]
     int e1 = exponent1 - 15;<-------I don't understand this part at all either [refer Ans06. The same logic goes here]
     int e2 = exponent2 - 15;<-------I don't understand this part at all either [refer Ans06. The same logic goes here]
     float frac1 = ((float) (source1 & 0x7f));<-------I don't understand this part at all either [refer Ans05]
     float frac2 = ((float) (source2 & 0x7f));<-------I don't understand this part at all either
     // the fractions with the leading 1 <-------Why is this special?
     frac1 = (frac1 / ((float)pow(2, 7))) + 1; <-------I don't understand this part at all either [refer Ans04, same this is here]
     frac2 = (frac2 / ((float)pow(2, 7))) + 1;
     // check to see which exponent is better <-------I don't understand this part at all either
     if (e1 > e2)
      {
        int i;
        int delta = e1 - e2; <-------What's delta?
        for (i = 0; i < delta; ++i)<-------I don't understand this part at all either
           { // justify frac 2<-------I don't understand this part at all either
             frac2 = frac2 / 2;
             ++e2;
           }
       }
      else if (e1 < e2)<-------I don't understand this part at all either [Ans13: decide which number is bigger]
       {
         int i;
         int delta = e2 - e1;
         for ( i = 0; i < delta; ++i)
            { // justify frac1<-------I don't understand this part at all either [Ans14: You can add or subtract two numbers only if their exponent is same. so this adjustment is needed.]
              frac1 = frac1 / 2;
              ++e1;
            }
        }

      float frac = frac2 + frac1;
      // if the fraction is greater than 2 justify frac down<-------I don't understand this part at all either
      // and increment the exponent. <-------I don't understand this part at all either
      while (frac >= 2)<-------I don't understand this part at all either [See Ans10 same applies here]
         {
           frac = frac / 2;<-------I don't understand this part at all either [See Ans10 same applies here]
           ++e1;
         }
      // if the fraction is less than 1 justify up<-------I don't understand this part at all either [already explained]
      // decrement the exponent.<-------I don't understand this part at all either [already explained]
      while (frac < 1)<-------I don't understand this part at all either [already explained]
         {
           frac = frac * 2;<-------I don't understand this part at all either [already explained]
           --e1;<-------I don't understand this part at all either [already explained]
         }
      if ((e1 + 15) > 30) return -1;
      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);
}

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

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