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); printf("The value of the two Mantissas added: %x\n", addMantissa); // 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. if (addMantissa > 0x7fffff) { addMantissa >> 1; ++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); }



LinkBack URL
About LinkBacks


