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.
  • M Offline
    M Offline
    mybm1
    wrote on last edited by
    #1

    #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 :( :( :( :(

    L C enhzflepE S 4 Replies 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 :( :( :( :(

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

      Your summation function makes no sense. You pass in a parameter which is ignored and then do some fairly random calculations, and return the value which will be zero, after a single iteration of the loop.

      M 1 Reply Last reply
      0
      • L Lost User

        Your summation function makes no sense. You pass in a parameter which is ignored and then do some fairly random calculations, and return the value which will be zero, after a single iteration of the loop.

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

        so as far as u say which way i should proceed? actually my logic was not working so was planning to implement kahan summation.

        L 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 :( :( :( :(

          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #4

          :omg: What are you doing? OK, you are trying to implement the "Kahan summation algorithm"[^]. However you have to read carefully and understand it, before actually coding. For instance, as shown by the algorithm pseudo-code[^], the parameter to the summation function must be an array, while you are using a float.

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

          M 1 Reply Last reply
          0
          • M mybm1

            so as far as u say which way i should proceed? actually my logic was not working so was planning to implement kahan summation.

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

            maibam debina wrote:

            so as far as u say which way i should proceed?

            Well the first thing you need to do is to define what the summation function is supposed to do; I cannot make any sense out of it. Once you have done that, then define the steps required and convert them to code. It would probably help you immensely to use meaningful names for your variables rather than single letters which make it difficult to understand.

            M 1 Reply Last reply
            0
            • L Lost User

              maibam debina wrote:

              so as far as u say which way i should proceed?

              Well the first thing you need to do is to define what the summation function is supposed to do; I cannot make any sense out of it. Once you have done that, then define the steps required and convert them to code. It would probably help you immensely to use meaningful names for your variables rather than single letters which make it difficult to understand.

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

              can u please help me out with code...:(

              L 1 Reply Last reply
              0
              • C CPallini

                :omg: What are you doing? OK, you are trying to implement the "Kahan summation algorithm"[^]. However you have to read carefully and understand it, before actually coding. For instance, as shown by the algorithm pseudo-code[^], the parameter to the summation function must be an array, while you are using a float.

                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
                #7

                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.

                C 1 Reply Last reply
                0
                • M mybm1

                  can u please help me out with code...:(

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

                  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 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 :( :( :( :(

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

                    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 1 Reply Last reply
                    0
                    • 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
                            • 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
                              #14

                              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

                                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
                                #15

                                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

                                C M 2 Replies 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.

                                  C Offline
                                  C 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

                                  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

                                    C Offline
                                    C 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

                                    M enhzflepE 2 Replies Last reply
                                    0
                                    • C 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?

                                      C 1 Reply Last reply
                                      0
                                      • C 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
                                          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