can anyone help how to find summation of float number?
-
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++ operatornew
#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;
}
-
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.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 .
-
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 .
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? -
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? -
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.
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
-
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?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...:(
-
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.
#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 -
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
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
-
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
-
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
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:
-
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:
-
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?
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
-
can u please kindly explain your assumption?becoz een though i put %.2f its doesnot seem much change in the answer that y..
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)
-
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...:(
-
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
-
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
-
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 tosquare
, is this another function in your code? -
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
-
#include
#include
#includefloat 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 :( :( :( :(
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);