My Computer Forum Computer Science Forum

Go Back   My Computer Forum > Computer Science Forum > Programming

Programming Programming Languages, Interpreters, and Compilers - Assembly, Declarative, Machine, Object Oriented, Iterative


Reply
 
LinkBack Thread Tools Display Modes
August 30th, 2009, 03:08 PM   #1
 
Joined: Oct 2008
Posts: 16
Summation with C

I have the following code:

Code:
#include <stdio.h>

int main (void)
{

	float accumulator = 0.0;
	int index;
	
	for (index = 0; index < 1000000; index++)
		accumulator += 1.0/(index*index+1);

	printf("Result ==> %f\n", accumulator);

}
This code is intended to give an approximation of the sum of 1/(1 + n^2) over n=1 to n=infinity. However, using approximation with definite integrals I have determined that the exact value of this sum lies between ?/4 and ?/2 (inclusive) but the output of the program is 17.071898. If you can spot an error in my code or point out some detail that I am missing I would be grateful.

Note: The method I used for approximation with definite integrals is given here.
greg1313 is offline  
 

My Computer Forum is free to register and we welcome everyone!

September 2nd, 2009, 04:24 PM   #2
 
Joined: Sep 2009
Posts: 4
Re: Summation with C

I coded virtually the same, it appears to be heading towards 1.076 or so (Borland compiler)
I can't spot the problem
my code:

#include <fstream.h> //for cout

void main()
{
unsigned int i5,maxx;
float summ;
summ=0.0;
maxx=1000;

for (i5=1;i5<maxx;i5++)
{summ=summ+1./(i5*i5+1);
if(i5%50==0){cout <<" sum = " << summ << endl;}
}

return;
}
billymac00 is offline  
September 2nd, 2009, 06:58 PM   #3
 
Joined: Oct 2008
Posts: 16
Re: Summation with C

Yes, that's the correct answer, thanks. I think my error might be in the formatting flags or something along those lines. I'll have to study the manual some more. BTW I'm using gcc 4.3.2 on Debian GNU/Linux 5.0. Thanks again.
greg1313 is offline  
September 4th, 2009, 08:22 PM   #4
 
Joined: Sep 2009
Posts: 4
Re: Summation with C

ok, I copied your code w/ very slight mods for an online compiler
it prints accumul at each iteration ...
via this site (which I have never used before), it seems that the sum tending towards 2.075672
ie only 1 too much

http://www.delorie.com/djgpp/compile/ (it produces a compiled exe for download but maybe not Linux)

maybe this helps?

a 2nd site produces online results but requires registration: http://www.botskool.com/online-compiler

2nd site gave same results obviously ...
billymac00 is offline  
September 5th, 2009, 09:48 AM   #5
 
Joined: Oct 2008
Posts: 16
Re: Summation with C

I made a boo-boo. The initial value for the index variable should be 1.

The following is correct:

Code:
#include <stdio.h>

int main (void)
{

	float accumulator = 0.0;
	int index;
	
	for (index = 1; index < 1000000; index++)
		accumulator += 1.0/(index*index+1);

	printf("Result ==> %f\n", accumulator);

}
Anyhow, that accounts for the result being off by 1. I'm beginning to suspect there's a bug somewhere but that the code is ok. Yikes!

Thanks for the help.
greg1313 is offline  
June 9th, 2010, 10:54 PM   #6
 
Joined: May 2010
Posts: 23
Re: Summation with C

You should rather take long integer and double float.
Syntax:
long int "int name"
double float "float name"
chetanbhasin is offline  
Reply

  My Computer Forum > Computer Science Forum > Programming

Tags
summation



Thread Tools
Display Modes






Copyright © 2018 My Computer Forum Forum. All rights reserved.