how to get sum of float number in c ? like if i enter 12.56 then answer should be 14

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dhruvin
    New Member
    • Sep 2013
    • 1

    how to get sum of float number in c ? like if i enter 12.56 then answer should be 14

    how to get sum of float number in c ? like if i enter 12.56 then answer should be 14
  • stdq
    New Member
    • Apr 2013
    • 94

    #2
    The following program does that, with the number being stored in an array of chars instead of a float variable. The -48 is used due to the fact that in ASCII the integer 48 represents the char '0'. I'm not completely sure this program is 100% correct, though.

    Code:
    #include <stdio.h>
    
    int main( void )
    {
        char numberAsString[] = "12.56";
        
        int i;
        int sum = 0;
        
        for ( i = 0 ; numberAsString[ i ] != '\0' ; i++ )
        {
            if ( numberAsString[ i ] != '.' )
            {
                sum = sum + ( int )( numberAsString[ i ] ) - 48;
            }
        }
        
        printf( "The sum is %d.\n", sum );
        
        return 0;    
    }

    Comment

    • Nepomuk
      Recognized Expert Specialist
      • Aug 2007
      • 3111

      #3
      To answer the question more generally: You probably won't be able to do that due to the way in which floating point numbers are stored in languages like C and C++. The paper What Every Computer Scientist Should Know About Floating Point Arithmetic from 1991 explains this in detail but the gist of it is this: You're going to get rounding errors. Now, getting the digits before the comma should be easy; you can just cast the number to int. When you have an int you can calculate the digit sum by doing something like this:
      [code=C]int number = 125; // The number you want to calculate the digit sum of. We're assuming it's positive.
      int digit_sum = 0; // The value in which the digit sum will be saved
      while(number >= 10) {
      digit_sum += number % 10; // The modulo operator will deliver the rest that occurs while dividing by the second number - in this case 10. That would be the last digit.
      number /= 10; // Now that we have added the last digit, we can "remove" it by dividing by 10
      }
      digit_sum += number; // The loop runs until "number" is smaller than 10. So, there's just one digit left. Add that to the digit sum[/code] This can be modified to work with floating point numbers to an extend, but how accurate your result will be I do not know.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Rather than using - 48 you would be better off using - '0' as this will work regardless of the executable character set in use.

        On to how to do it, assuming that the number is in range for an integer then, as Nepomuk says you can do the bit to the left of the decimal point by converting to an integer and using the modulus (%) and division operators to separate out individual digits.

        After that doing the fractional part, right of the decimal point, I think you are going to need an idea of how many digits you want to include in the calculation, the issue being if you think think of you floating point number 12.56 the computer could be holding either 12.559999999999 or 12.560000000001 . In either case you are actually only interested in the value to 2 digits.

        If the integer part has been dealt with then we are left with either 0.559999999999 or 0.560000000001. But if we know we want only 2 decimal places we can times by 10^2 (100) add 0.5 and convert to an integer.

        [code]
        0.559999999999 0.560000000001
        *100 55.9999999999 56.0000000001
        +0.5 56.4999999999 56.5000000001
        convert to int 56 56

        And then repeat the procedure that we used on the initial integer part. In fact in we know that the size of the original floating point number is small enough then we can do this bit first and end up with an integer containing 1256.

        Another option would be to put it in a string and then process the characters of the string as stdq suggested.

        The main limitation of both of these is that you need to know how many decimal places are in floating point number you wish to convert and that may not be easy to work out.

        Some pseudo code like this might work

        Code:
        tolerance = 0.0000001
        number = approx(12.56)
        number = absolute_value(number)
        places = 0;
        stop = false
        while((!stop) && (places != 15))
        {
           high = number - integer(number)
           low = (integer(number) + 1) - number
           if ((high < tolerance) || (low < tolerance))
           {
              stop = true
           }
           else
           {
             number = number - integer(number)
             number = number * 10
             places = places + 1
           }
        }

        Comment

        Working...