In: Computer Science
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);
   }
}
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 ###############################

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