Thread: Addition Program - Help

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    22

    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);
    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);
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You need to shift the littleMan to the right, not to the left.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    22
    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);
    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 > 0xFFFFF)//0x7fffff)
    {
        addMantissa = 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);
    }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    22
    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. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And since you throw away the top three bits of addMantissa, you expected what else? (Again: 0xfffff is only 20 bits.)

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    22
    I see the mistake , thank you . Let me get back to correcting this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  2. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  3. Replies: 13
    Last Post: 01-13-2008, 09:38 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM