Question

In: Computer Science

COMPUTER SCIENCE- FLOATING POINT REPRESENTATION: Hello, I completed a program for floating point representation (it can...

COMPUTER SCIENCE- FLOATING POINT REPRESENTATION:

Hello, I completed a program for floating point representation (it can add and multiply floating point values in IEEE format). I already completed it, but I came back with 3 small errors. Can someone please fix them? I posted them at the bottom, here is the code:

__________________________________________________________________________________________________________________________________

fp.java:

// fp class
public class fp {

   // add function
public int add(int a, int b) {
FPNumber fa = new FPNumber(a);
FPNumber fb = new FPNumber(b);
FPNumber result = new FPNumber(0);

//***************************************************************************************************
// addition- handle exceptions


if (fa.isNaN() || fb.isNaN()) {
return (fa.isNaN() ? fa : fb).asInt();
}


if (fa.isZero()) {
return fb.asInt();
} else if (fb.isZero()) {
return fa.asInt();
}


if (fa.isInfinity() && fb.isInfinity()) {
if (fa.s() == fb.s()) {
return fa.asInt();
} else {
result.setE(255);
result.setF(1);
return fa.asInt();
}
}


if (fa.isInfinity()) {
return fa.asInt();
} else if (fb.isInfinity()) {
return fb.asInt();
}

//***************************************************************************************************
// addition- sort numbers


FPNumber fa2 = new FPNumber(a);
FPNumber fb2 = new FPNumber(b);


if (fa._e != fb._e) {
fa2 = (fa.e() > fb.e()) ? fa : fb;
fb2 = (fa.e() < fb.e()) ? fa : fb;
} else {
fa2 = (fa.f() > fb.f()) ? fa : fb;
fb2 = (fa.f() < fb.f()) ? fa : fb;
}

//***************************************************************************************************
// addition- align exponents
if (fa2.e() != fb2.e()) {
int shift = fa2._e - fb2._e;
// if the difference between A-B's exponent > 24, return A's value
if (shift > 24) {
return fa2.asInt();
}

long temp = fb2._f >> shift;

fb2.setE(fa2._e);
fb2.setF(temp);

}

//***************************************************************************************************
// addition- add or subtract
long mantissaMod;

// if Asign = Bsign, we add the mantissas
// if Asign ≠ Bsign, we subtract B from A
if (fa2.s() == fb2.s()) {
mantissaMod = fa2.f() + fb2.f();
} else {
mantissaMod = fa2.f() - fb2.f();


if (mantissaMod == 0) {
fa2.setE(0);
fa2.setF(0);

return fa2.asInt();
}
}

result.setS(fa2._s);
//***************************************************************************************************
// addition- normalize
do {
if (((1 << 26) & mantissaMod) != 0) {
mantissaMod >>= 1;
fa2._e++;
}


if (fa2._e >= 255) {
result.setE(255);
result.setF(0);
return result.asInt();
}


while (((1 << 25) & mantissaMod) == 0) {
mantissaMod <<= 1;
fa2._e--;


if (fa2._e <= 0) {
result.setF(mantissaMod >> 1);
result.setE(0);
return result.asInt();
}
}

long low2bit = mantissaMod & 3;
if (low2bit != 0) {
mantissaMod += 4;
}


} while (((1 << 25) & mantissaMod) == 0);

result.setF(mantissaMod);
result.setE(fa2._e);

return result.asInt();
}

//***************************************************************************************************

// multiply function
public int mul(int a, int b){
    FPNumber fa = new FPNumber(a);
    FPNumber fb = new FPNumber(b);
    FPNumber result = new FPNumber(0);

//***************************************************************************************************
// multiply- handle exceptions
if (fa.isNaN() || fb.isNaN()) {
return (fa.isNaN() ? fa : fb).asInt();
}


if(fa.isZero() && fb.isInfinity()){
    result.setE(255);
    result.setF(1);
    return result.asInt();
} else if(fb.isZero() && fa.isInfinity()){
    result.setE(255);
    result.setF(1);
    return result.asInt();
}


result.setS((fa.s() != fb.s()) ? -1 : 1);


if(fa.isZero() || fb.isZero()){
    return result.asInt();
}


if(fa.isInfinity() || fb.isInfinity()){
result.setE(255);
return result.asInt();
}

//***************************************************************************************************
// multiply- add the exponents

// temp exponent value
int temp_e = fa.e() + fb.e() - 127;

if(temp_e > 254) {
result.setE(255);
return result.asInt();
}else if(temp_e < 0) {
return result.asInt();
}
//***************************************************************************************************
// multiply- muliply mantissas


long mantissaMod = fa.f() * fb.f();
mantissaMod >>= 26;
temp_e+=1;

//***************************************************************************************************
// multiplication- normalize

do {
if (((1 << 26) & mantissaMod) != 0) {
mantissaMod >>= 1;
temp_e++;
}
if (temp_e >= 255) {
result.setE(255);
result.setF(0);
return result.asInt();
}


while (((1 << 25) & mantissaMod) == 0) {
mantissaMod <<= 1;
temp_e--;

if (temp_e <= 0) {
result.setF(mantissaMod >> 1);
result.setE(0);

return result.asInt();
}
}

long low2bit = mantissaMod & 3;
if (low2bit != 0) {
mantissaMod += 4;
}

} while (((1 << 25) & mantissaMod) == 0);

result.setF(mantissaMod);
result.setE(temp_e);

return result.asInt();
}

//***************************************************************************************************

// driver class
public static void main(String[] args) {

fp m = new fp();

//***************************************************************************************************
// TESTING THAT LED TO ERRORS

// ERROR 1: add same pos and neg number, result zero (EXPECTED 0, GOT -259.25)

int v129_625 = 0x4301A000; // 129.625
int v_129625 = 0xC301A000; // -129.625

System.out.println(Float.intBitsToFloat(m.add(v129_625, v_129625)) + " should be 0");

// ERROR 2: add plus and minus infinity (EXPECTED NaN, GOT minus infinity)

int vINF = 0x7F800000;
int _vINF = 0xFF800000;

System.out.println(Float.intBitsToFloat(m.add(vINF, _vINF)) + " should be NaN");


// ERROR 3: mul infinity and zero (EXPECTED NaN, GOT zero)

int zero = 0x00000000;


System.out.println(Float.intBitsToFloat(m.add(vINF, zero)) + " should be NaN");


}

}

____________________________________________________________________________________________________

FPNumber.java

public class FPNumber{

   int _s, _e;
   long _f;

   public FPNumber(int a){
       _s = (((a >> 31) & 1) == 1) ? -1 : 1;
       _e = (a >> 23) & 0xFF;
       _f = a & 0x7FFFFF;
   if (_e != 0 && _e != 255){
       _f |= 0x0800000;
   }
       _f <<= 2;
   }

// setters and getters
   public int s(){
       return _s;
   }

   public int e(){
   return _e;
   }

   public long f(){
   return _f;
   }

   public void setS(int val){
   _s = val;
   }

   public void setE(int val){
   _e = val;
   }

   public void setF(long val){
   _f = val;
   }

   public boolean isNaN(){
   return _e == 255 && _f > 0;
   }

   public boolean isInfinity(){
   return _e == 5 && _f == 0;
   }

   public boolean isZero(){
   return _e == 0 && _f == 0;
   }

   public int asInt(){
   return ((_s == -1) ? 0x80000000 : 0) | (_e << 23) | (((int) _f >> 2) & 0x07FFFFF);
   }

}

Solutions

Expert Solution

Summary :

Modified files are pasted below : files fp.java , FPNumber.java & output for 3 examples given in main .

Note : fixed add and FPNumber calculations . for the 3rd example of Infinity + 0 , I observe online resources point the result to be Infinity , please check and revert if there are any clarifications.

Added few output statements to troubleshoot .

####################### fp.java ############################

public class fp {

   // add function
public int add(int a, int b) {
FPNumber fa = new FPNumber(a);

FPNumber fb = new FPNumber(b);

FPNumber result = new FPNumber(0);

//***************************************************************************************************
// addition- handle exceptions

//fa.printInfo();
//fb.printInfo();

   if (fa.isNaN() || fb.isNaN()) {
       return (fa.isNaN() ? fa : fb).asInt();
   }


   if (fa.isZero()) {
       return fb.asInt();
   } else if (fb.isZero()) {
       return fa.asInt();
   }


   if (fa.isInfinity() && fb.isInfinity()) {
       //System.out.println(" ############# Both are Infinity ");
       if (fa.s() == fb.s()) {
           //System.out.println(" ############# Both are Infinity and Signs are equal");
           return fa.asInt();
       } else {
       //System.out.println(" ############# Both are Infinity and Signs not equal");
           result.setE(255);
           result.setF(0x7FFFFFFF);
           //result.printInfo();
           return result.asInt();
       }
   }


   if (fa.isInfinity()) {
       return fa.asInt();
   } else if (fb.isInfinity()) {
       return fb.asInt();
   }

//***************************************************************************************************
// addition- sort numbers


FPNumber fa2 = new FPNumber(a);
FPNumber fb2 = new FPNumber(b);

//System.out.println(" #### 0 A2 & B2#### " );
//fa2.printInfo();
//fb2.printInfo();


if (fa._e != fb._e) {
fa2 = (fa.e() > fb.e()) ? fa : fb;
fb2 = (fa.e() < fb.e()) ? fb : fa;
} else {
fa2 = (fa.f() > fb.f()) ? fa : fb;
fb2 = (fa.f() < fb.f()) ? fb : fa;
}

//System.out.println(" #### 1 A2 & B2#### " );
//fa2.printInfo();
//fb2.printInfo();

//***************************************************************************************************
// addition- align exponents
if (fa2.e() != fb2.e()) {
   int shift = fa2._e - fb2._e;
   // if the difference between A-B's exponent > 24, return A's value
   if (shift > 24) {
       return fa2.asInt();
   }

   long temp = fb2._f >> shift;

   fb2.setE(fa2._e);
   fb2.setF(temp);
   //System.out.println("###### Exponent alingment \n");
}

//System.out.println(" ####2 A2 & B2#### " );
//fa2.printInfo();
//fb2.printInfo();
//***************************************************************************************************
// addition- add or subtract
long mantissaMod;

// if Asign = Bsign, we add the mantissas
// if Asign ≠ Bsign, we subtract B from A
//System.out.println("##### Sign a2 : " + fa2.s() + "   b2 S : " + fb2.s() );
if (fa2.s() == fb2.s()) {
mantissaMod = fa2.f() + fb2.f();
//System.out.println("####****** Mantessa : " + mantissaMod );
} else {
   mantissaMod = fa2.f() - fb2.f();

   //System.out.println("#### Mantessa : " + mantissaMod );
   if (mantissaMod == 0) {
       fa2.setE(0);
       fa2.setF(0);

       return fa2.asInt();
   }
}

result.setS(fa2._s);
//***************************************************************************************************
// addition- normalize
do {
if (((1 << 26) & mantissaMod) != 0) {
mantissaMod >>= 1;
fa2._e++;
}


if (fa2._e >= 255) {
result.setE(255);
result.setF(0);
return result.asInt();
}


while (((1 << 25) & mantissaMod) == 0) {
mantissaMod <<= 1;
fa2._e--;


if (fa2._e <= 0) {
result.setF(mantissaMod >> 1);
result.setE(0);
return result.asInt();
}
}

long low2bit = mantissaMod & 3;
if (low2bit != 0) {
mantissaMod += 4;
}


} while (((1 << 25) & mantissaMod) == 0);

result.setF(mantissaMod);
result.setE(fa2._e);

return result.asInt();
}

//***************************************************************************************************

// multiply function
public int mul(int a, int b){
    FPNumber fa = new FPNumber(a);
    FPNumber fb = new FPNumber(b);
    FPNumber result = new FPNumber(0);

//***************************************************************************************************
// multiply- handle exceptions
if (fa.isNaN() || fb.isNaN()) {
return (fa.isNaN() ? fa : fb).asInt();
}


if(fa.isZero() && fb.isInfinity()){
    result.setE(255);
    result.setF(0x7FFFFFFF);
    return result.asInt();
} else if(fb.isZero() && fa.isInfinity()){
    result.setE(255);
    result.setF(0x7FFFFFFF);
    return result.asInt();
}


result.setS((fa.s() != fb.s()) ? -1 : 1);


if(fa.isZero() || fb.isZero()){
    return result.asInt();
}


if(fa.isInfinity() || fb.isInfinity()){
result.setE(255);
return result.asInt();
}

//***************************************************************************************************
// multiply- add the exponents

// temp exponent value
int temp_e = fa.e() + fb.e() - 127;

if(temp_e > 254) {
result.setE(255);
return result.asInt();
}else if(temp_e < 0) {
return result.asInt();
}
//***************************************************************************************************
// multiply- muliply mantissas


long mantissaMod = fa.f() * fb.f();
//mantissaMod >>= ;
//mantissaMod = mantissaMod & 0x7FFFFF;
//temp_e+=1;


//***************************************************************************************************
// multiplication- normalize

System.out.println("Exponents : " + temp_e + " Mantessha : " + mantissaMod);


do {
if (((1 << 26) & mantissaMod) != 0) {
mantissaMod >>= 1;
temp_e++;
}

if (temp_e >= 255) {
result.setE(255);
result.setF(0);
return result.asInt();
}


while (((1 << 25) & mantissaMod) == 0) {
mantissaMod <<= 1;
temp_e--;

if (temp_e <= 0) {
result.setF(mantissaMod >> 1);
result.setE(0);

return result.asInt();
}
}

long low2bit = mantissaMod & 3;
if (low2bit != 0) {
mantissaMod += 4;
}

} while (((1 << 25   ) & mantissaMod) == 0);

result.setF(mantissaMod);
result.setE(temp_e);

return result.asInt();
}

//***************************************************************************************************

// driver class
public static void main(String[] args) {

fp m = new fp();

//***************************************************************************************************
// TESTING THAT LED TO ERRORS

// ERROR 1: add same pos and neg number, result zero (EXPECTED 0, GOT -259.25)

int v129_625 = 0x4301A000; // 129.625
int v_129625 = 0xC301A000; // -129.625

System.out.println(Float.intBitsToFloat(m.add(v129_625, v_129625)) + " should be 0");

// ERROR 2: add plus and minus infinity (EXPECTED NaN, GOT minus infinity)

int vINF = 0x7F800000;
int _vINF = 0xFF800000;

System.out.println(Float.intBitsToFloat(m.add(vINF, _vINF)) + " should be NaN");


// ERROR 3: mul infinity and zero (EXPECTED NaN, GOT zero)

int zero = 0x00000000;


System.out.println(Float.intBitsToFloat(m.add(vINF, zero)) + " should be NaN");


}

}

###########################End FP.java #################################

############################### FPNumber.java #############################

public class FPNumber{

   int _s, _e;
   long _f;
   int hex ;

   public FPNumber(int a){
       hex = a;
       _f = (a) & 0x7FFFFF;
       _s = (((a >> 31) & 1) == 1) ? -1 : 1;
       _e = (a >> 23) & 0xFF;
       //_f = (a >> 9) & 0x7FFFFF;
       //if (_e != 0 && _e != 255){
       //   _f |= 0x0800000;
       //   }
       //_f <<= 2;
   }

// setters and getters
   public int s(){
       return _s;
   }

   public int e(){
   return _e;
   }

   public long f(){
   return _f;
   }

   public void setS(int val){
   _s = val;
   }

   public void setE(int val){
   _e = val;
   }

   public void setF(long val){
   _f = val;
   }

   public boolean isNaN(){
   return _e == 255 && _f > 0;
   }

   public boolean isInfinity(){
   return _e == 255 && _f == 0;
   }

   public boolean isZero(){
   return _e == 0 && _f == 0;
   }

   public int asInt(){
   return ((_s == -1) ? 0x80000000 : 0) | (_e << 23) | (((int) _f >> 2) & 0x07FFFFF);
   }

   void printInfo() {
       System.out.println( "HEX : " + this.hex + " S : " + this.s() + " _E : " + this.e() + " F: " + this.f() );
       System.out.println( " IsZero : " + this.isZero() + " , IsInfinity : " + this.isInfinity() + " , isNan : " + this.isNaN() );
     
   }

   public static void main(String[] args) {
       int v129_625 = 0x4301A000; // 129.625
       int v_129625 = 0xC301A000; // -129.625
       int v_85_125 = 0x42AA4000; // 85.125
      
       FPNumber fa = new FPNumber(v129_625); fa.printInfo();
   //   System.out.println( " X " + this " S : " + fa.s() + " _E : " + fa.e() + " F: " + fa.f() );
       FPNumber fa1 = new FPNumber(v_129625); fa1.printInfo();
   //   System.out.println( " S : " + fa1.s() + " _E : " + fa1.e() + " F: " + fa1.f() );
      
   }

}

################################# End FPNumber.java #########################

##################################### Output ###############################

#############################################################################


Related Solutions

Consider the following 32-bit floating point representation based on the IEEE floating point standard: There is...
Consider the following 32-bit floating point representation based on the IEEE floating point standard: There is a sign bit in the most significant bit. The next eight bits are the exponent, and the exponent bias is 28-1-1 = 127. The last 23 bits are the fraction bits. The representation encodes number of the form V = (-1)S x M x 2E, where S is the sign, M is the significand, and E is the biased exponent. The rules for the...
The biggest mysteries of the IEEE 754 Floating-Point Representation are “hidden bit” and “Bias. Can someone...
The biggest mysteries of the IEEE 754 Floating-Point Representation are “hidden bit” and “Bias. Can someone explain to me why the "hidden bits" and "bias" are considered to be mysteries for the IEEE 754 floating point representation
3. IEEE Floating Point Representation What decimal number does the 32-bit IEEE floating point number 0xC27F0000...
3. IEEE Floating Point Representation What decimal number does the 32-bit IEEE floating point number 0xC27F0000 represent? Fill in the requested information in the blanks below. What is the sign of the number (say positive or negative): What is the exponent in decimal format: What is the significand in binary: What is the value of the stored decimal number in decimal (final answer): Credit will be given for your final answer in the blanks and the work shown below.
Convert the following number into 32bit IEEE 754 floating point representation. 0.000101
Convert the following number into 32bit IEEE 754 floating point representation. 0.000101
Determine the IEEE single and double floating point representation of the following numbers: a) -26.25 b)...
Determine the IEEE single and double floating point representation of the following numbers: a) -26.25 b) 15/2
These questions concern the following 16-bit floating point representation: The first bit is the sign of...
These questions concern the following 16-bit floating point representation: The first bit is the sign of the number (0 = +, 1 = -), the next nine bits are the mantissa, the next bit is the sign of the exponent, and the last five bits are the magnitude of the exponent. All numbers are normalized, i.e. the first bit of the mantissa is one, except for zero which is all zeros. a) What is the largest number? (in both 16-bit...
Concern the following 16-bit floating point representation: The first bit is the sign of the number...
Concern the following 16-bit floating point representation: The first bit is the sign of the number (0 = +, 1 = -), the next nine bits are the mantissa, the next bit is the sign of the exponent, and the last five bits are the magnitude of the exponent. All numbers are normalized, i.e. the first bit of the mantissa is one, except for zero which is all zeros. 1. What's the smallest difference between two consecutive or adjacent numbers?...
Q1: In the addition of floating-point numbers, how do we adjust the representation of numbers with...
Q1: In the addition of floating-point numbers, how do we adjust the representation of numbers with different exponents? Q2: Answer the following questions: What binary operation can be used to set bits? What bit pattern should the mask have? What binary operation can be used to unset bits? What bit pattern should the mask have? What binary operation can be used to flip bits? What bit pattern should the mask have?
I have a program to code for my computer science class, and Im not sure how...
I have a program to code for my computer science class, and Im not sure how to do it. If someone can explain it step by step I would appreciate it. Problem Description: Define the GeometricObject2D class that contains the properties color and filled and their appropriate getter and setter methods. This class also contains the dateCreated property and the getDateCreated() and toString() methods. The toString() method returns a string representation of the object. Define the Rectangle2D class that extends...
Determine the IEEE single and double floating point representation of the following numbers: a) (15/2) x...
Determine the IEEE single and double floating point representation of the following numbers: a) (15/2) x 2^50 b) - (15/2) x 2^-50 c) 1/5
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT