1. ## Addition Program - Help

The following code , should compute two floating point number and return the correct result . That is the problem ......the addition is not correct. I do not see the coding error ? Any suggestions ? Thank you.

Code:
```#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <assert.h>

int isNegative (float f)
{
unsigned int* iptr = (unsigned int*)&f;
return ( ((*iptr) & 0x80000000) ? 1:0);
}

unsigned char getExponent (float f)  // Purpose is to return the 8 bit exponent of the floating point value
{
unsigned int* iptr = (unsigned int*)&f;
return (((*iptr >> 23) & 0xff)-127) ;

}

unsigned int getMantissa (float f) // Purpose to return the 24 bit mantissa of the floating point value.
{
unsigned int* iptr = (unsigned int*)&f;
if( *iptr == 0 ) return 0;
return ((*iptr & 0xFFFFFF) | 0x800000 );

}

float sum (float left, float right) // Purpose to return the sum of the floating point values left & right .
{
// Will obtain the exponents of the left & right and will obtain the mantissa.
unsigned int littleMan;
unsigned int bigMan;
unsigned char littleExp;
unsigned char bigExp;
unsigned char lexp = getExponent(left);
unsigned char rexp = getExponent(right);

if (lexp > rexp)
{
bigExp = lexp;
bigMan = getMantissa(left);
littleExp = rexp;
littleMan = getMantissa(right);
}
else
{
bigExp = rexp;
bigMan = getMantissa(right);
littleExp = lexp;
littleMan = getMantissa(left);
}

printf("little: %x %x\n", littleExp, littleMan);
printf("big:    %x %x\n", bigExp, bigMan);

//Purpose is to extract difference in exponet values to determin how much to shift the mantissa
int expSub = (bigExp - littleExp);
printf("Subtraction of the Exp: %x\n", expSub);

// Purpose is shift the mantissas to allign binary points.
int shifta = (littleMan << expSub);
printf("The value of the Exp after the shift:  %x\n", shifta);

// Purpose is to add mantissas
int addMantissa = (bigMan + shifta);

// Purpose is if the mantissa is too big , extending into the 24 bit , shift over to to fit mantissa and update bigExp to compensate for the shift and strip the hidden bit.
{
++bigExp;
}

// Purpose is to reassemble the floating point number

unsigned int result =  (expSub + 127)<<23 | (addMantissa & 0xfffff) ;
printf ("This is the addition:  %x\n", result);

float fresult = *(float*)&result;
return(fresult);

}

int main()
{
const int SIZE = 256;
char line[SIZE];

while (1)
{
float f1;
float f2;
float left = f1;
float right = f2;

printf("Please enter the first float ( \"q\" to quit):");
fgets(line,SIZE,stdin);

if (toupper(line[0]) =='Q')
break;

f1 = atof(line);

printf("Please enter the second float ( \"q\" to quit):");
fgets(line,SIZE,stdin);

if (toupper(line[0]) == 'Q')
break;

f2 = atof(line);

if (isNegative(f1) || isNegative(f2))
printf ("One of thse is negative, but %g + %g == %g\n", f1,f2,sum(f1,f2));
else
printf("%g + %g == %g\n", f1,f2,sum(f1,f2));
}

return(EXIT_SUCCESS);
}```

2. You need to shift the littleMan to the right, not to the left.

3. OK, I have made some changes to this program but it is still out putting an incorrect answer ? Am I writing my "if" statement correctly , I think this is where the problem but can not ID it exactly ?

Code:
```#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <assert.h>

int isNegative (float f)
{
unsigned int* iptr = (unsigned int*)&f;
return ( ((*iptr) & 0x80000000) ? 1:0);
}

unsigned char getExponent (float f)  // Purpose is to return the 8 bit exponent of the floating point value
{
unsigned int* iptr = (unsigned int*)&f;
return (((*iptr >> 23) & 0xff)) ;

}

unsigned int getMantissa (float f) // Purpose to return the 24 bit mantissa of the floating point value.
{
unsigned int* iptr = (unsigned int*)&f;
if( *iptr == 0 ) return 0;
return ((*iptr & 0xFFFFFF) | 0x800000 );

}

float sum (float left, float right) // Purpose to return the sum of the floating point values left & right .
{
// Will obtain the exponents of the left & right and will obtain the mantissa.
unsigned int littleMan;
unsigned int bigMan;
unsigned char littleExp;
unsigned char bigExp;
unsigned char lexp = getExponent(left);
unsigned char rexp = getExponent(right);

if (lexp > rexp)
{
bigExp = lexp;
bigMan = getMantissa(left);
littleExp = rexp;
littleMan = getMantissa(right);
}
else
{
bigExp = rexp;
bigMan = getMantissa(right);
littleExp = lexp;
littleMan = getMantissa(left);
}

printf("little: %x %x\n", littleExp, littleMan);
printf("big:    %x %x\n", bigExp, bigMan);

//Purpose is to extract difference in exponet values to determin how much to shift the mantissa
int expSub = (bigExp - littleExp);
printf("Subtraction of the Exp: %x\n", expSub);

// Purpose is shift the mantissas to allign binary points.
int shifta = (littleMan >> expSub);
printf("The value of the Exp after the shift:  %x\n", shifta);

// Purpose is to add mantissas
int addMantissa = (bigMan + shifta);

// Purpose is if the mantissa is too big , extending into the 24 bit , shift over to to fit mantissa and update bigExp to compensate for the shift and strip the hidden bit.
{
++bigExp;
}

// Purpose is to reassemble the floating point number

unsigned int result =  (expSub + 127)<<23 | (addMantissa & 0xfffff) ;
printf ("This is the addition:  %x\n", result);

float fresult = *(float*)&result;
return(fresult);

}

int main()
{
const int SIZE = 256;
char line[SIZE];

while (1)
{
float f1;
float f2;
float left = f1;
float right = f2;

printf("Please enter the first float ( \"q\" to quit):");
fgets(line,SIZE,stdin);

if (toupper(line[0]) =='Q')
break;

f1 = atof(line);

printf("Please enter the second float ( \"q\" to quit):");
fgets(line,SIZE,stdin);

if (toupper(line[0]) == 'Q')
break;

f2 = atof(line);

if (isNegative(f1) || isNegative(f2))
printf ("One of thse is negative, but %g + %g == %g\n", f1,f2,sum(f1,f2));
else
printf("%g + %g == %g\n", f1,f2,sum(f1,f2));
}

return(EXIT_SUCCESS);
}```

4. So as you're printing things out, where does it go wrong? And, since you realize you should have 0x7fffff instead of 0xfffff, why did you not actually fix that?

5. Right in this section

Code:
```// Purpose is to reassemble the floating point number

unsigned int result =  (expSub + 127)<<23 | (addMantissa & 0xfffff) ;
printf ("This is the addition:  &#37;x\n", result);

float fresult = *(float*)&result;
return(fresult);```

6. And since you throw away the top three bits of addMantissa, you expected what else? (Again: 0xfffff is only 20 bits.)

7. I see the mistake , thank you . Let me get back to correcting this.