Advertisement

PI calculator

Started by February 18, 2003 09:19 PM
14 comments, last by Digigamer15 21 years, 9 months ago
I''m trying to make a program in c++ that desplays pi on the screen and keeps going (infinately if possible) the problem is that I do it, it only desplays pi to 5 decimal points due to the restrictions of "double" can anyone tell me how I would go about making it infinate (or at least a lot more then 5)? Or can it not be done? Thanks ----------------------------- Programming is confussing! AIM: Trebor DoD
-----------------------------AIM: Trebor DoDHompage: Thinking Digitally: My Web Blog
I haven''t tried it, but it says it can handle up to 2^32 (4294967295) digits.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
Advertisement
It''s large, it''s C, it''s not mine, but may help.

/*
* PI - program to calculate ã to N places *
* 1st argument is the number of places desired. Space is allocated *
* by dynamic allocation. *
* *
* Programmed by Bill Davidsen after the method of G. M. Roe, *
* based on a version for "E" supplied with the "B" compiler, 1970 *
* *
* Modified by Alexander Morris, September 9, 1987 to be fully *
* compatible with Borland''s Turbo C *
* *
* Modified by Jeff Smith, June 14, 1990 to allow a file name to *
* be supplied as the 2nd argument, so that the output can be *
* viewed later; also, messages are output to inform the user of *
* what is going on. *
* *
* Modified by Mariano Egurrola, July 22, 1994 replacing dynamic *
* tables by files in order to avoid memory limitations, and * *
* adding processing time report. *
*/

#include <stdio.h>
#include <conio.h>
#include <process.h>
#include <stdlib.h>
#include <dos.h>

const SIZE = sizeof(long);

long kf, ks;
long cnt, n, i, temp, nd;
long col, col1;
long loc, stor[21];
long par_shift1, par_shift2;
FILE *fp;
int y;

FILE *mf_fp;
FILE *ms_fp;

long mf_num;
long *mf_num_ptr;

long ms_num;
long *ms_num_ptr;

struct date date_proc;
struct time time_proc;

main (int argc, char *argv[])

{

mf_num_ptr=&mf_num;
ms_num_ptr=&ms_num;

col = col1;

stor[i++] = 0; /* also init */

if (argc < 2) {
fprintf (stderr, "Format is: pi \n");
fprintf(stderr,"Range for n is 1 to %li.\n",2147483647);
exit(0);
}

if (argc == 3)
if((fp = fopen(argv[2],"w")) == NULL) {
fprintf(stderr,"Unable to open %s.",argv[2]);
exit(0);
}

n = atol (argv[1]);

if(n<1|n>2147483647) {
fprintf(stderr,"Range for n is 1 to %li.\n",2147483647);
exit(0);
}

system("del MF.*");
system("del MS.*");

if((mf_fp=fopen("MF.DAT","w+b"))==NULL)
return(1);

if((ms_fp=fopen("MS.DAT","w+b"))==NULL)
return(1);

clrscr();
printf("PI - Program to calculate ã to n places.\n\n");

getdate(&date_proc);
gettime(&time_proc);

printf("Process started : %d/%d/%d %d:%d:%d\n\n",
date_proc.da_day, date_proc.da_mon, date_proc.da_year,
time_proc.ti_hour,time_proc.ti_min,time_proc.ti_sec);

if(!fp)
printf(" 3.");

cnt = 0;
kf = 25;
ks = 57121L;

mf_num = 0;
fwrite(mf_num_ptr, SIZE, 1, mf_fp);
ms_num = 0;
fwrite(ms_num_ptr, SIZE, 1, ms_fp);

mf_num = 1;
fwrite(mf_num_ptr, SIZE, 1, mf_fp);

for (i = 2; i <= n; i += 2) {

mf_num = -16;
fwrite(mf_num_ptr, SIZE, 1, mf_fp);

mf_num = 16;
fwrite(mf_num_ptr, SIZE, 1, mf_fp);

}

for (i = 1; i <= n; i += 2) {

ms_num = -4;
fwrite(ms_num_ptr, SIZE, 1, ms_fp);

ms_num = 4;
fwrite(ms_num_ptr, SIZE, 1, ms_fp);

}


if(fp) {
fprintf(fp,"PI - Program to calculate ã to n places.\n\n");
fprintf(fp,
"********************************************************************\n"
"* Programmed by Bill Davidsen after the method of G. M. Roe, *\n"
"* based on a version for \"E\" supplied with the \"B\" compiler, 1970 *\n"
"* *\n"
"* Modified by Alexander Morris, September 9, 1987 to be fully *\n"
"* compatible with Borland''s Turbo C *\n"
"* *\n"
"* Modified by Jeff Smith, June 14, 1990 to allow a file name to *\n"
"* be supplied as the 2nd argument, so that the output can be *\n"
"* viewed later; also, messages are output to inform the user of *\n"
"* what is going on. *\n"
"* *\n"
"* Modified by Mariano Egurrola, July 22, 1994 replacing dynamic *\n"
"* tables by files in order to avoid memory limitations, and *\n"
"* adding processing time report. *\n"
"********************************************************************\n");
fprintf(fp,"\nProcess started : %d/%d/%d %d:%d:%d\n",
date_proc.da_day, date_proc.da_mon, date_proc.da_year,
time_proc.ti_hour,time_proc.ti_min,time_proc.ti_sec);
fprintf(fp,"\n Printing ã to %ld places....\n\n",n);
printf(" Printing ã to %ld places....\n\n",n);
y = wherey();
fprintf(fp," 3.");
}

while (cnt < n) {
if (fp) {
gotoxy(1,y);
printf(" Calculating position %li...",cnt+1);
}

for (i = 0; ++i <= n - cnt {
fseek(mf_fp, (i*SIZE),0);
fread(mf_num_ptr, SIZE, 1, mf_fp);
mf_num *= 10;
fseek(mf_fp, (i*SIZE),0);
fwrite(mf_num_ptr, SIZE, 1, mf_fp);

fseek(ms_fp, (i*SIZE),0);
fread(ms_num_ptr, SIZE, 1, ms_fp);
ms_num *= 10;
fseek(ms_fp, (i*SIZE),0);
fwrite(ms_num_ptr, SIZE, 1, ms_fp);

}

for (i = n - cnt + 1; --i >= 2 {

temp = 2 * i - 1;

fseek(mf_fp,((i-1)*SIZE),0);
fread(mf_num_ptr, SIZE, 1, mf_fp);
par_shift1 = mf_num;

fseek(mf_fp,(i*SIZE),0);
fread(mf_num_ptr, SIZE, 1, mf_fp);
par_shift2 = mf_num;

shift (∥_shift1,∥_shift2, temp - 2, temp * kf);

fseek(mf_fp,(i*SIZE),0);
mf_num=par_shift2;
fwrite(mf_num_ptr, SIZE, 1, mf_fp);

fseek(mf_fp,((i-1)*SIZE),0);
mf_num=par_shift1;
fwrite(mf_num_ptr, SIZE, 1, mf_fp);

fseek(ms_fp,((i-1)*SIZE),0);
fread(ms_num_ptr, SIZE, 1, ms_fp);
par_shift1 = ms_num;

fseek(ms_fp,(i*SIZE),0);
fread(ms_num_ptr, SIZE, 1, ms_fp);
par_shift2 = ms_num;

shift (∥_shift1,∥_shift2, temp - 2, temp * ks);

ms_num=par_shift2;
fseek(ms_fp,(i*SIZE),0);
fwrite(ms_num_ptr, SIZE, 1, ms_fp);

fseek(ms_fp,((i-1)*SIZE),0);
ms_num=par_shift1;
fwrite(ms_num_ptr, SIZE, 1, ms_fp);

} nd = 0;

fseek(mf_fp, SIZE, 0);
fread(mf_num_ptr, SIZE, 1, mf_fp);
par_shift2 = mf_num;

shift (&nd, ∥_shift2, 1L, 5L);

fseek(mf_fp, SIZE, 0);
mf_num = par_shift2;
fwrite(mf_num_ptr, SIZE, 1, mf_fp);

fseek(ms_fp, SIZE, 0);
fread(ms_num_ptr, SIZE, 1, ms_fp);
par_shift2 = ms_num;

shift (&nd, ∥_shift2, 1L, 239L);

fseek(ms_fp, SIZE, 0);
ms_num = par_shift2;
fwrite(ms_num_ptr, SIZE, 1, ms_fp);

xprint (nd);
}

getdate(&date_proc);
gettime(&time_proc);

if (fp) {
fprintf(fp,"\n\nProcess end : %d/%d/%d %d:%d:%d\n",
date_proc.da_day, date_proc.da_mon, date_proc.da_year,
time_proc.ti_hour,time_proc.ti_min,time_proc.ti_sec);
fclose(fp);
printf(" Calculation position %li...\n",n);
printf(" Results stored in file - %s.\n",argv[2]);
}

printf("\n\nProcess end : %d/%d/%d %d:%d:%d\n\n",
date_proc.da_day, date_proc.da_mon, date_proc.da_year,
time_proc.ti_hour,time_proc.ti_min,time_proc.ti_sec);

fclose(mf_fp);
fclose(ms_fp);
}

shift (l1, l2, lp, lmod)
long *l1, *l2, lp, lmod;
{
long k;

k = ((*l2) > 0 ? (*l2) / lmod : -(-(*l2) / lmod) - 1);
*l2 -= k * lmod;
*l1 += k * lp;
par_shift1 = *l1;
par_shift2 = *l2;
}

yprint (m)
long m;
{
if (cnt < n) {
if (++col == 6) {
col = 1;
if (++col1 == 10) {
col1 = 0;
if (!fp) printf ("\n%4d", m % 10);
else fprintf (fp,"\n%4d", m % 10);
}
else
if (!fp) printf ("%2d", m % 10);
else fprintf (fp,"%2d", m % 10);
}
else
if (!fp) printf ("%d", m);
else fprintf (fp,"%d", m);
cnt++;
}
}

xprint (m)
long m;
{
long i, wk, wk1;

if (m < 8) {
for (i = 1; i <= loc
yprint (stor[i++]);
loc = 0;
}
else
if (m > 9) {
wk = m / 10;
m %= 10;
for (wk1 = loc; wk1 >= 1; wk1--) {
wk += stor[wk1];
stor[wk1] = wk % 10;
wk /= 10;
}
}
stor[++loc] = m;
}

-----------------------------------------------------------Web Comic
ack! exepotes i tried the code that you recommended and got 46 errors....

-----------------------------
Programming is confussing!
AIM: Trebor DoD
-----------------------------AIM: Trebor DoDHompage: Thinking Digitally: My Web Blog
ugh, probably you are using VC++, and it has a lot of incompatibilities with Turbo C (Borland). I think they (Borland) made that compiler freeware long ago. search the web to find it, and it should compile without problems. also, the code is just to get the idea.
or you can try with another compiler (Dev C++/Mingw comes to mind). it may work (or don''t)
I don''t know, I''m watching the errors and they don''t look impossible to fix.
-----------------------------------------------------------Web Comic
I went through and spaced it all out for you and fixed some of the errors I knew how. I got it to work.. I think... I don''t know if the values are right or not but they look convincing to me.


  /** PI - program to calculate ã to N places ** 1st argument is the number of places desired. Space is allocated ** by dynamic allocation. ** ** Programmed by Bill Davidsen after the method of G. M. Roe, ** based on a version for "E" supplied with the "B" compiler, 1970 ** ** Modified by Alexander Morris, September 9, 1987 to be fully ** compatible with Borland''s Turbo C ** ** Modified by Jeff Smith, June 14, 1990 to allow a file name to ** be supplied as the 2nd argument, so that the output can be ** viewed later; also, messages are output to inform the user of ** what is going on. ** ** Modified by Mariano Egurrola, July 22, 1994 replacing dynamic ** tables by files in order to avoid memory limitations, and * ** adding processing time report. **/#include <stdio.h>#include <conio.h>#include <process.h>#include <stdlib.h>#include <dos.h>void xprint( long m);void shift( long *l1, long *l2, long lp, long lmod );void yprint ( long m);const SIZE = sizeof(long);long kf, ks;long cnt, n, i, temp, nd;long col, col1;long loc, stor[21];long par_shift1, par_shift2;FILE *fp;int y;FILE *mf_fp;FILE *ms_fp;long mf_num;long *mf_num_ptr;long ms_num;long *ms_num_ptr;main (int argc, char *argv[]){	mf_num_ptr=&mf_num;	ms_num_ptr=&ms_num;	col = col1;	stor[i++] = 0; /* also init */	if (argc < 2)	{		fprintf (stderr, "Format is: pi \n");		fprintf(stderr,"Range for n is 1 to %li.\n",2147483647);		exit(0);	}	if (argc == 3)		if((fp = fopen(argv[2],"w")) == NULL)		{			fprintf(stderr,"Unable to open %s.",argv[2]);			exit(0);		}	n = atol (argv[1]);	if( (n<1)|| (n>2147483647))	{		fprintf(stderr,"Range for n is 1 to %li.\n",2147483647);		exit(0);	}	system("del MF.*");	system("del MS.*");	if((mf_fp=fopen("MF.DAT","w+b"))==NULL)		return(1);	if((ms_fp=fopen("MS.DAT","w+b"))==NULL)		return(1);	printf("PI - Program to calculate ã to n places.\n\n");		printf("Process started now\n\r");	if(!fp)		printf(" 3.");	cnt = 0;	kf = 25;	ks = 57121L;	mf_num = 0;	fwrite(mf_num_ptr, SIZE, 1, mf_fp);	ms_num = 0;	fwrite(ms_num_ptr, SIZE, 1, ms_fp);	mf_num = 1;	fwrite(mf_num_ptr, SIZE, 1, mf_fp);	for (i = 2; i <= n; i += 2)	{		mf_num = -16;		fwrite(mf_num_ptr, SIZE, 1, mf_fp);		mf_num = 16;		fwrite(mf_num_ptr, SIZE, 1, mf_fp);	}	for (i = 1; i <= n; i += 2)	{		ms_num = -4;		fwrite(ms_num_ptr, SIZE, 1, ms_fp);		ms_num = 4;		fwrite(ms_num_ptr, SIZE, 1, ms_fp);	}	if(fp)	{		fprintf(fp,"PI - Program to calculate ã to n places.\n\n");		fprintf(fp,		"********************************************************************\n"		"* Programmed by Bill Davidsen after the method of G. M. Roe, *\n"		"* based on a version for \"E\" supplied with the \"B\" compiler, 1970 *\n"		"* *\n"		"* Modified by Alexander Morris, September 9, 1987 to be fully *\n"		"* compatible with Borland''s Turbo C *\n"		"* *\n"		"* Modified by Jeff Smith, June 14, 1990 to allow a file name to *\n"		"* be supplied as the 2nd argument, so that the output can be *\n"		"* viewed later; also, messages are output to inform the user of *\n"		"* what is going on. *\n"		"* *\n"		"* Modified by Mariano Egurrola, July 22, 1994 replacing dynamic *\n"		"* tables by files in order to avoid memory limitations, and *\n"		"* adding processing time report. *\n"		"********************************************************************\n");		fprintf(fp,"\nProcess started : now\n" );		fprintf(fp,"\n Printing ã to %ld places....\n\n",n);		printf(" Printing ã to %ld places....\n\n",n);//		y = wherey();		fprintf(fp," 3.");	}	while (cnt < n)	{		if (fp)		{//			gotoxy(1,y);			printf(" Calculating position %li...",cnt+1);		}		for (i = 0; ++i <= n - cnt; )		{			fseek(mf_fp, (i*SIZE),0);			fread(mf_num_ptr, SIZE, 1, mf_fp);			mf_num *= 10;			fseek(mf_fp, (i*SIZE),0);			fwrite(mf_num_ptr, SIZE, 1, mf_fp);			fseek(ms_fp, (i*SIZE),0);			fread(ms_num_ptr, SIZE, 1, ms_fp);			ms_num *= 10;			fseek(ms_fp, (i*SIZE),0);			fwrite(ms_num_ptr, SIZE, 1, ms_fp);		}		for (i = n - cnt + 1; --i >= 2; )		{			temp = 2 * i - 1;			fseek(mf_fp,((i-1)*SIZE),0);			fread(mf_num_ptr, SIZE, 1, mf_fp);			par_shift1 = mf_num;			fseek(mf_fp,(i*SIZE),0);			fread(mf_num_ptr, SIZE, 1, mf_fp);			par_shift2 = mf_num;			shift (∥_shift1,∥_shift2, temp - 2, temp * kf);			fseek(mf_fp,(i*SIZE),0);			mf_num=par_shift2;			fwrite(mf_num_ptr, SIZE, 1, mf_fp);			fseek(mf_fp,((i-1)*SIZE),0);			mf_num=par_shift1;			fwrite(mf_num_ptr, SIZE, 1, mf_fp);				fseek(ms_fp,((i-1)*SIZE),0);			fread(ms_num_ptr, SIZE, 1, ms_fp);			par_shift1 = ms_num;			fseek(ms_fp,(i*SIZE),0);			fread(ms_num_ptr, SIZE, 1, ms_fp);			par_shift2 = ms_num;			shift (∥_shift1,∥_shift2, temp - 2, temp * ks);			ms_num=par_shift2;			fseek(ms_fp,(i*SIZE),0);			fwrite(ms_num_ptr, SIZE, 1, ms_fp);			fseek(ms_fp,((i-1)*SIZE),0);			ms_num=par_shift1;			fwrite(ms_num_ptr, SIZE, 1, ms_fp);		}				nd = 0;		fseek(mf_fp, SIZE, 0);		fread(mf_num_ptr, SIZE, 1, mf_fp);		par_shift2 = mf_num;		shift (&nd, ∥_shift2, 1L, 5L);			fseek(mf_fp, SIZE, 0);		mf_num = par_shift2;		fwrite(mf_num_ptr, SIZE, 1, mf_fp);		fseek(ms_fp, SIZE, 0);		fread(ms_num_ptr, SIZE, 1, ms_fp);		par_shift2 = ms_num;		shift (&nd, ∥_shift2, 1L, 239L);		fseek(ms_fp, SIZE, 0);		ms_num = par_shift2;		fwrite(ms_num_ptr, SIZE, 1, ms_fp);		xprint (nd);	}	if (fp)	{		fprintf(fp,"\n\nProcess end : now\n" );		fclose(fp);		printf(" Calculation position %li...\n",n);		printf(" Results stored in file - %s.\n",argv[2]);	}	printf("\n\nProcess end : now\n\n");	fclose(mf_fp);	fclose(ms_fp);	return 0;}void shift( long *l1, long *l2, long lp, long lmod ){	long k;	k = ((*l2) > 0 ? (*l2) / lmod : -(-(*l2) / lmod) - 1);	*l2 -= k * lmod;	*l1 += k * lp;	par_shift1 = *l1;	par_shift2 = *l2;}void yprint ( long m){	if (cnt < n)	{		if (++col == 6)		{			col = 1;			if (++col1 == 10)			{				col1 = 0;				if (!fp)					printf ("\n%4d", m % 10);				else					fprintf (fp,"\n%4d", m % 10);			}			else			if (!fp)				printf ("%2d", m % 10);			else				fprintf (fp,"%2d", m % 10);		}		else if (!fp)			printf ("%d", m);		else			fprintf (fp,"%d", m);					cnt++;	}}void xprint( long m){	long i, wk, wk1;	if (m < 8)	{		for (i = 1; i <= loc; i++ )			yprint (stor[i]);		loc = 0;	}	else if (m > 9)	{		wk = m / 10;		m %= 10;		for (wk1 = loc; wk1 >= 1; wk1--)		{			wk += stor[wk1];			stor[wk1] = wk % 10;			wk /= 10;		}	}	stor[++loc] = m;}  


btw, the higher the precision, the longer it takes to calculate EACH position. Be prepared to wait a long time!!! 32 decimal places took about 1 second... 100 about 5 seconds, 1000 about 3 minutes... it claims to be able to calculate up to 2147483647 decimal places. Go away for a week and leave it running and let me know if it does

btw pi =
3.14159 26535 89793 23846 26433 83279 50288 41971 69399 37510   58209 74944 59230 78164 06286 20899 86280 34825 34211 70679   82148 08651 32823 06647 09384 46095 50582 23172 53594 08128   48111 74502 84102 70193 85211 05559 64462 29489 54930 38196   44288 10975 66593 34461 28475 64823 37867 83165 27120 19091   45648 56692 34603 48610 45432 66482 13393 60726 02491 41273   72458 70066 06315 58817 48815 20920 96282 92540 91715 36436   78925 90360 01133 05305 48820 46652 13841 46951 94151 16094   33057 27036 57595 91953 09218 61173 81932 61179 31051 18548   07446 23799 62749 56735 18857 52724 89122 79381 83011 94912   98336 73362 44065 66430 86021 39494 63952 24737 19070 21798   60943 70277 05392 17176 29317 67523 84674 81846 76694 05132   00056 81271 45263 56082 77857 71342 75778 96091 73637 17872   14684 40901 22495 34301 46549 58537 10507 92279 68925 89235   42019 95611 21290 21960 86403 44181 59813 62977 47713 09960   51870 72113 49999 99837 29780 49951 05973 17328 16096 31859   50244 59455 34690 83026 42522 30825 33446 85035 26193 11881   71010 00313 78387 52886 58753 32083 81420 61717 76691 47303   59825 34904 28755 46873 11595 62863 88235 37875 93751 95778   18577 80532 17122 68066 13001 92787 66111 95909 21642 01989 



I got this to compile and work on MSVC++ 6.0 btw, not sure what your using.
Evillive2
Advertisement

here

is a link to pick up the exe if you don''t want to bother with compiling it.

console command line syntax:
>pi [n] [filename]

[n] is the number of decimal places, [filename] is the output file that recieves the numbers. The output to the screen is really annoying through this so you might want to redirect that to a different file as well.
Evillive2
I'm almost tempted to let it run all night and see how far it gets. 1000 decimal places only took like 10 seconds, so I'll give it a try.


Edit: Err nevermind, I set it at 1 million decimal places and it was going hella slow.

[edited by - energy_dan on February 19, 2003 2:14:59 AM]
"Alright Dan, I need you to finish this within the hour, and I need you to do it correctly....Oh yeah one more thing, Don't F*** Me!"- My Boss
Just to add my 2 cents...

If the 5 digits are annyoing you some other time, keep in mind there is a thing called cout.precision(intHere);
hahaha.. I have a dog of a computer at 500MhZ... I left it running when I went to bed last night and it took just under 6 hours to compute 10,000 places.

On a side note, this could be a nice benchmark tool for testing out computer performance. i.e. Pentium vs similar AMD system if you change the timer precision to something more like milliseconds.

EDIT: here is a link to the modified C files and executable I used. Got rid of some of the annoying screen output and added better timer support.


[edited by - evillive2 on February 19, 2003 11:37:37 AM]
Evillive2

This topic is closed to new replies.

Advertisement