can anyone help how to find summation of float number?
-
#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 :( :( :( :(
-
#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 :( :( :( :(
-
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. -
#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 :( :( :( :(
: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
-
so as far as u say which way i should proceed? actually my logic was not working so was planning to implement kahan summation.
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. -
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. -
: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
-
#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 :( :( :( :(
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;
}
-
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? -
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...:(
-
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
-
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: