Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. can anyone help how to find summation of float number?

can anyone help how to find summation of float number?

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
28 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • enhzflepE enhzflep

    Consider the following implementation of a summation function and various ways of creating/populating an array for it to work on. 1. static data declared at compile time 2. dynamic data allocated with the c function malloc 3. dynamic data allocated with the c++ operator new

    #include <cstdlib>
    #include <cstdio>

    float sumArrayElements(float array[], int numItems)
    {
    int i;
    float result = 0;
    for (i=0; i<numItems; i++)
    result += array[i];
    return result;
    }

    int main ()
    {
    float array1[5] = {10.2, 13.7, 14.8, 99.0, 0.0};
    float arraySum1 = sumArrayElements( array1, 5);
    printf("Array sum: %.2f\n",arraySum1);

    int i;
    
    float \*array2;
    int dynamicCount1 = 5;
    array2 = (float\*)malloc(dynamicCount1 \* sizeof(float) );
    for (i=0; i<dynamicCount1; i++)
        array2\[i\] = (float)rand()/(float)(RAND\_MAX);
    float arraySum2 = sumArrayElements( array2, dynamicCount1);
    free(array2);
    printf("Array sum: %.2f\\n",arraySum2);
    
    
    int dynamicCount2 = 10;
    float \*array3 = new float\[dynamicCount2\];
    for (i=0; i<dynamicCount2; i++)
        array3\[i\] = (float)rand()/(float)(RAND\_MAX);
    for (i=0; i<dynamicCount1; i++)
        array3\[i\] = (float)rand()/(float)(RAND\_MAX);
    float arraySum3 = sumArrayElements( array3, dynamicCount2);
    delete array3;
    printf("Array sum: %.2f\\n",arraySum3);
    
    return 0;
    

    }

    M Offline
    M Offline
    mybm1
    wrote on last edited by
    #10

    thanks for your contribution but this is the logic i apllied for summation 'result +=array[i]' but this is giving the approximate answer not the exact one.

    enhzflepE 1 Reply Last reply
    0
    • L Lost User

      Well I could, but i) I have no idea what your summation function is supposed to do, and ii) you would learn more by trying it yourself.

      M Offline
      M Offline
      mybm1
      wrote on last edited by
      #11

      Actually the logic which i use to implement for performing the summation was

      Quote:

      for(i=0;i<5;i++)
      { //float y,sum;
      float f= ((float)rand()/(float)(RAND_MAX))*a;
      y=square(f);
      sum +=y;
      }
      printf("\n Summation Of Square Number is:=%f\t\n",sum);

      but it doesnot seem well its not giving the exact summation value,all i want was to perform summation of the generated float random numbers .

      L 1 Reply Last reply
      0
      • M mybm1

        Actually the logic which i use to implement for performing the summation was

        Quote:

        for(i=0;i<5;i++)
        { //float y,sum;
        float f= ((float)rand()/(float)(RAND_MAX))*a;
        y=square(f);
        sum +=y;
        }
        printf("\n Summation Of Square Number is:=%f\t\n",sum);

        but it doesnot seem well its not giving the exact summation value,all i want was to perform summation of the generated float random numbers .

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #12

        Are you sure that sum contains zero at the beginning of the loop? In the statement:

        float f= ((float)rand()/(float)(RAND_MAX))*a;

        what is the value of a? And since you are not printing any of the intervening values, how can you be sure the final sum is not correct?

        M 2 Replies Last reply
        0
        • L Lost User

          Are you sure that sum contains zero at the beginning of the loop? In the statement:

          float f= ((float)rand()/(float)(RAND_MAX))*a;

          what is the value of a? And since you are not printing any of the intervening values, how can you be sure the final sum is not correct?

          M Offline
          M Offline
          mybm1
          wrote on last edited by
          #13

          a=0.2

          1 Reply Last reply
          0
          • M mybm1

            thanks for your contribution but this is the logic i apllied for summation 'result +=array[i]' but this is giving the approximate answer not the exact one.

            enhzflepE Offline
            enhzflepE Offline
            enhzflep
            wrote on last edited by
            #14

            Except for a rather small subset of numbers, calculations with computers involving floating-point numbers are approximate. Integer arithmetic doesn't suffer from this pitfall. However, if you are talking about the result of the code I posted - then consider reviewing the printf statements - in each of them I have used the %.2f format specifier to tell printf to only print 2 digits after the decimal place. The point is - the answer is as close to exact as floats will give you, it is only the display that is (grossly) approximate. :) You can get a less approximate result printed if you change the print specifiers to %f

            CPalliniC M 2 Replies Last reply
            0
            • L Lost User

              Are you sure that sum contains zero at the beginning of the loop? In the statement:

              float f= ((float)rand()/(float)(RAND_MAX))*a;

              what is the value of a? And since you are not printing any of the intervening values, how can you be sure the final sum is not correct?

              M Offline
              M Offline
              mybm1
              wrote on last edited by
              #15

              for(i=0;i<5;i++)
              {
              float f= ((float)rand()/(float)(RAND_MAX))*a; //rand return integer value from 0 to RAND_MAX(system dependent)
              //float y[5];

                      printf("%f\\t",f);
              
              
                      y=square(f);
                      printf("\\t\\t%f\\n",y);
                      
                         }
              
              
              
              
                  for(i=0;i<5;i++)
                               {  //float y,sum;
                              float f= ((float)rand()/(float)(RAND\_MAX))\*a;
                              y=square(f);
                                  sum +=y;
                           }
                  printf("\\n Summation Of Square Number is:=%f\\t\\n",sum);
              

              i have print at the 1st for loop...:(

              L 1 Reply Last reply
              0
              • M mybm1

                i tried using array but seem it not working can u please help me out with code making modification...actual thing what i want is to do the summation of all the generated random number which is store at b.

                CPalliniC Offline
                CPalliniC Offline
                CPallini
                wrote on last edited by
                #16

                #include

                #define N 5

                double summation(double g[], int size);

                int main()
                {
                double a[N] =
                {
                0.035679,
                0.079935,
                0.033320,
                0.042424,
                0.012387
                };

                double s[N];

                int i;
                for (i=0; i
                THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?!
                -- C++ FQA Lite

                In testa che avete, signor di Ceprano?

                1 Reply Last reply
                0
                • enhzflepE enhzflep

                  Except for a rather small subset of numbers, calculations with computers involving floating-point numbers are approximate. Integer arithmetic doesn't suffer from this pitfall. However, if you are talking about the result of the code I posted - then consider reviewing the printf statements - in each of them I have used the %.2f format specifier to tell printf to only print 2 digits after the decimal place. The point is - the answer is as close to exact as floats will give you, it is only the display that is (grossly) approximate. :) You can get a less approximate result printed if you change the print specifiers to %f

                  CPalliniC Offline
                  CPalliniC Offline
                  CPallini
                  wrote on last edited by
                  #17

                  As far as I can understand, he is trying to imlpement the "Kahan summation algorithm"[^].

                  THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

                  In testa che avete, signor di Ceprano?

                  M enhzflepE 2 Replies Last reply
                  0
                  • CPalliniC CPallini

                    As far as I can understand, he is trying to imlpement the "Kahan summation algorithm"[^].

                    THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

                    M Offline
                    M Offline
                    mybm1
                    wrote on last edited by
                    #18

                    CPallini i have given just for 5 number as demo but in real my count is above 60000 ?and i am searching for the summation for all the 60000+ value which give correct result.it wouldt be possible for what?

                    CPalliniC 1 Reply Last reply
                    0
                    • CPalliniC CPallini

                      As far as I can understand, he is trying to imlpement the "Kahan summation algorithm"[^].

                      THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

                      enhzflepE Offline
                      enhzflepE Offline
                      enhzflep
                      wrote on last edited by
                      #19

                      Seems like a reasonable assumption to me. I'd seen your mention of it earlier, but hadn't seen a confirmation of this so guessed (wrongly?) where the problem lay. Thanks for the impetus to re-investigate the method - I seem to remember using it for something a while back, but brain-rot defeats me.. :sigh:

                      M 1 Reply Last reply
                      0
                      • enhzflepE enhzflep

                        Seems like a reasonable assumption to me. I'd seen your mention of it earlier, but hadn't seen a confirmation of this so guessed (wrongly?) where the problem lay. Thanks for the impetus to re-investigate the method - I seem to remember using it for something a while back, but brain-rot defeats me.. :sigh:

                        M Offline
                        M Offline
                        mybm1
                        wrote on last edited by
                        #20

                        can u please kindly explain your assumption?becoz een though i put %.2f its doesnot seem much change in the answer that y..

                        enhzflepE 1 Reply Last reply
                        0
                        • M mybm1

                          CPallini i have given just for 5 number as demo but in real my count is above 60000 ?and i am searching for the summation for all the 60000+ value which give correct result.it wouldt be possible for what?

                          CPalliniC Offline
                          CPalliniC Offline
                          CPallini
                          wrote on last edited by
                          #21

                          You cannot modify my program for processing more items, can you? :rolleyes:

                          THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

                          In testa che avete, signor di Ceprano?

                          M 1 Reply Last reply
                          0
                          • M mybm1

                            can u please kindly explain your assumption?becoz een though i put %.2f its doesnot seem much change in the answer that y..

                            enhzflepE Offline
                            enhzflepE Offline
                            enhzflep
                            wrote on last edited by
                            #22

                            Certainly. Both your code and the pseudo-code in the wiki page that CPallini linked to appear quite similar. This, combined with your mention of the result only being an approximation seems to further support your need for a more accurate result of summing. (which the method may facilitate)

                            1 Reply Last reply
                            0
                            • M mybm1

                              for(i=0;i<5;i++)
                              {
                              float f= ((float)rand()/(float)(RAND_MAX))*a; //rand return integer value from 0 to RAND_MAX(system dependent)
                              //float y[5];

                                      printf("%f\\t",f);
                              
                              
                                      y=square(f);
                                      printf("\\t\\t%f\\n",y);
                                      
                                         }
                              
                              
                              
                              
                                  for(i=0;i<5;i++)
                                               {  //float y,sum;
                                              float f= ((float)rand()/(float)(RAND\_MAX))\*a;
                                              y=square(f);
                                                  sum +=y;
                                           }
                                  printf("\\n Summation Of Square Number is:=%f\\t\\n",sum);
                              

                              i have print at the 1st for loop...:(

                              L Offline
                              L Offline
                              Lost User
                              wrote on last edited by
                              #23

                              And in the second loop you will have a different set of numbers, as returned by the calls to rand(). I also cannot find a reference to square, is this another function in your code?

                              M 1 Reply Last reply
                              0
                              • CPalliniC CPallini

                                You cannot modify my program for processing more items, can you? :rolleyes:

                                THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

                                M Offline
                                M Offline
                                mybm1
                                wrote on last edited by
                                #24

                                :-D thank you finally i got it after full torture to u and others too..

                                CPalliniC 1 Reply Last reply
                                0
                                • enhzflepE enhzflep

                                  Except for a rather small subset of numbers, calculations with computers involving floating-point numbers are approximate. Integer arithmetic doesn't suffer from this pitfall. However, if you are talking about the result of the code I posted - then consider reviewing the printf statements - in each of them I have used the %.2f format specifier to tell printf to only print 2 digits after the decimal place. The point is - the answer is as close to exact as floats will give you, it is only the display that is (grossly) approximate. :) You can get a less approximate result printed if you change the print specifiers to %f

                                  M Offline
                                  M Offline
                                  mybm1
                                  wrote on last edited by
                                  #25

                                  omg u were right ,finally i got the problem was only with the where i gave the print statement..

                                  1 Reply Last reply
                                  0
                                  • L Lost User

                                    And in the second loop you will have a different set of numbers, as returned by the calls to rand(). I also cannot find a reference to square, is this another function in your code?

                                    M Offline
                                    M Offline
                                    mybm1
                                    wrote on last edited by
                                    #26

                                    ya it is thank fren i got it...matter was with the printf statement i gav at second loop ,wen i pass the printf statement inside the 1st loop itself n removing the second for loop as condition where same ...den i got the exact answer..

                                    1 Reply Last reply
                                    0
                                    • M mybm1

                                      :-D thank you finally i got it after full torture to u and others too..

                                      CPalliniC Offline
                                      CPalliniC Offline
                                      CPallini
                                      wrote on last edited by
                                      #27

                                      You are welcome.

                                      THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

                                      In testa che avete, signor di Ceprano?

                                      1 Reply Last reply
                                      0
                                      • M mybm1

                                        #include
                                        #include
                                        #include

                                        float summation(float g);
                                        int main(int argc,int *argv[])
                                        {
                                        srand(12345);
                                        float z;
                                        float a=0.2,y=0.0;
                                        int i;
                                        for(i=0;i<5;i++)
                                        {
                                        float b=((float)rand()/(float)(RAND_MAX))*a;
                                        printf("%f\n",b);

                                        z=summation(b);
                                        }
                                        printf("summation:\n%f\n",z);
                                        return 0;
                                        }
                                        //=========================================================
                                        float summation(float g)
                                        {
                                        int i;
                                        float y[5],sum =0.0;
                                        float c=0.0;

                                        		for(i=1;i<5;i++)
                                        			{
                                        				float z=y\[i\]-c;
                                        				float t = sum+z;
                                        				c=(t-sum)-z;
                                        				sum=t;
                                        				return sum;
                                        				//printf("\\n\\n%f\\t=",sum);
                                        			}
                                        

                                        }

                                        output 0.035679 0.079935 0.033320 0.042424 0.012387 summation: 0.000000 :( :( :( :(

                                        S Offline
                                        S Offline
                                        Satya Chamakuri
                                        wrote on last edited by
                                        #28

                                        hi, I don't know what's the use of summation in your code. If you want summation of float numbers generated numbers only means, the following piece of code is enough i guess.. See once

                                        srand(12345);
                                        float z=0.0;
                                        float a=0.2,y=0.0;
                                        int i;
                                        for(i=0;i<5;i++)
                                        {
                                        float b=((float)rand()/(float)(RAND_MAX))*a;
                                        printf("%f\n",b);

                                            z=z+b;
                                        }
                                        printf("summation:\\n%f\\n",z);
                                        
                                        1 Reply Last reply
                                        0
                                        Reply
                                        • Reply as topic
                                        Log in to reply
                                        • Oldest to Newest
                                        • Newest to Oldest
                                        • Most Votes


                                        • Login

                                        • Don't have an account? Register

                                        • Login or register to search.
                                        • First post
                                          Last post
                                        0
                                        • Categories
                                        • Recent
                                        • Tags
                                        • Popular
                                        • World
                                        • Users
                                        • Groups