From owner-freebsd-questions@FreeBSD.ORG Fri Nov 5 18:37:05 2010 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 19646106567A for ; Fri, 5 Nov 2010 18:37:05 +0000 (UTC) (envelope-from fidaj@ukr.net) Received: from fsm1.ukr.net (fsm1.ukr.net [195.214.192.23]) by mx1.freebsd.org (Postfix) with ESMTP id A6AA08FC0A for ; Fri, 5 Nov 2010 18:37:04 +0000 (UTC) Received: from 39-6-179-94.pool.ukrtel.net ([94.179.6.39] helo=localhost) by fsm1.ukr.net with esmtps ID 1PER9r-0004IM-Bl ; Fri, 05 Nov 2010 20:37:03 +0200 Date: Fri, 5 Nov 2010 20:37:02 +0200 From: Ivan Klymenko To: Alejandro Imass Message-ID: <20101105203702.1a364893@ukr.net> In-Reply-To: References: X-Mailer: Claws Mail 3.7.6 (GTK+ 2.20.1; amd64-portbld-freebsd9.0) Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: Arthur Bela , Remko Lodder , FreeBSD Mailing list Subject: Re: how to generate pi in c X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 05 Nov 2010 18:37:05 -0000 =D0=92 Fri, 5 Nov 2010 13:39:05 -0400 Alejandro Imass =D0=BF=D0=B8=D1=88=D0=B5=D1=82: > This is how I do it in perl > use constant PI =3D> 4 * atan2(1, 1); >=20 > In C it owuld probably be (using math.h): >=20 > pi =3D 4.0*atan(1.0); >=20 > On Fri, Nov 5, 2010 at 1:15 PM, Remko Lodder > wrote: > > > > No, but a simple search reveals some information; > > > > http://einstein.drexel.edu/courses/Comp_Phys/General/C_basics/ > > > > On Nov 5, 2010, at 5:40 PM, Arthur Bela wrote: > > > >> Does anyone has a "generate-pi.c" source code? > >> > >> Thanks.. :D :\ > >> > > > > -- > > /"\ =C2=A0 Best regards, =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0| remko@FreeBSD.org > > \ / =C2=A0 Remko Lodder =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0| > > X =C2=A0 =C2=A0http://www.evilcoder.org/ =C2=A0 =C2=A0| Quis custodiet = ipsos custodes > > / \ =C2=A0 ASCII Ribbon Campaign =C2=A0 =C2=A0| Against HTML Mail and N= ews > > > > > > > > > > _______________________________________________ > > freebsd-questions@freebsd.org mailing list > > http://lists.freebsd.org/mailman/listinfo/freebsd-questions > > To unsubscribe, send any mail to > > "freebsd-questions-unsubscribe@freebsd.org" > > > _______________________________________________ > freebsd-questions@freebsd.org mailing list > http://lists.freebsd.org/mailman/listinfo/freebsd-questions > To unsubscribe, send any mail to > "freebsd-questions-unsubscribe@freebsd.org" >=20 >=20 adapt the code to build on FreeBSD... excuse me - the problem in acceding file ///////////////////////////////////////////////////////////////////////////= ///////////////////////////////////////////////////////////////////////////= /// /* ** PI.C - Computes Pi to an arbitrary number of digits ** ** Uses far arrays so may be compiled in any memory model */ #include #include #if defined(__ZTC__) #include #define FAR _far #define Fcalloc farcalloc #define Ffree farfree #define Size_T unsigned long #elif defined(__TURBOC__) #include #define FAR far #define Fcalloc farcalloc #define Ffree farfree #define Size_T unsigned long #else /* assume MSC/QC */ #include #define FAR _far #define Fcalloc _fcalloc #define Ffree _ffree #define Size_T size_t #endif long kf, ks; long FAR *mf, FAR *ms; long cnt, n, temp, nd; long i; long col, col1; long loc, stor[21]; void shift(long FAR *l1, long FAR *l2, long lp, long lmod) { long k; k =3D ((*l2) > 0 ? (*l2) / lmod: -(-(*l2) / lmod) - 1); *l2 -=3D k * lmod; *l1 +=3D k * lp; } void yprint(long m) { if (cnt 9) { wk =3D m / 10; m %=3D 10; for (wk1 =3D loc; wk1 >=3D 1; wk1--) { wk +=3D stor[(int)wk1]; stor[(int)wk1] =3D wk % 10; wk /=3D 10; } } } stor[(int)(++loc)] =3D m; } void memerr(int errno) { printf("\a\nOut of memory error #%d\n", errno); if (2 =3D=3D errno) Ffree(mf); _exit(2); } int main(int argc, char *argv[]) { int i=3D0; char *endp; stor[i++] =3D 0; if (argc < 2) { puts("\aUsage: PI "); return(1); } n =3D strtol(argv[1], &endp, 10); if (NULL =3D=3D (mf =3D Fcalloc((Size_T)(n + 3L), (Size_T)sizeof(long= )))) memerr(1); if (NULL =3D=3D (ms =3D Fcalloc((Size_T)(n + 3L), (Size_T)sizeof(long= )))) memerr(2); printf("\nApproximation of PI to %ld digits\n", (long)n); cnt =3D 0; kf =3D 25; ks =3D 57121L; mf[1] =3D 1L; for (i =3D 2; i <=3D (int)n; i +=3D 2) { mf[i] =3D -16L; mf[i+1] =3D 16L; } for (i =3D 1; i <=3D (int)n; i +=3D 2) { ms[i] =3D -4L; ms[i+1] =3D 4L; } printf("\n 3."); while (cnt < n) { for (i =3D 0; ++i <=3D (int)n - (int)cnt; ) { mf[i] *=3D 10L; ms[i] *=3D 10L; } for (i =3D(int)(n - cnt + 1); --i >=3D 2; ) { temp =3D 2 * i - 1; shift(&mf[i - 1], &mf[i], temp - 2, temp * kf); shift(&ms[i - 1], &ms[i], temp - 2, temp * ks); } nd =3D 0; shift((long FAR *)&nd, &mf[1], 1L, 5L); shift((long FAR *)&nd, &ms[1], 1L, 239L); xprint(nd); } printf("\n\nCalculations Completed!\n"); Ffree(ms); Ffree(mf); return(0); } ///////////////////////////////////////////////////////////////////////////= ///////////////////////////////////////////////////////////////////////////= ///