Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 5 Nov 2010 20:37:02 +0200
From:      Ivan Klymenko <fidaj@ukr.net>
To:        Alejandro Imass <ait@p2ee.org>
Cc:        Arthur Bela <jozsi.avadkan@gmail.com>, Remko Lodder <remko@elvandar.org>, FreeBSD Mailing list <freebsd-questions@freebsd.org>
Subject:   Re: how to generate pi in c
Message-ID:  <20101105203702.1a364893@ukr.net>
In-Reply-To: <AANLkTik62acvRKx71sQ%2Bwqd%2BKb=auqb_NQkxT9F6y-Yr@mail.gmail.com>
References:  <AANLkTikaBYi0d4dvLnc=vxuPDWQHDqrVJrz=iVkECeO=@mail.gmail.com> <AFAF0F44-48A3-462A-8EA5-005E838AE44E@elvandar.org> <AANLkTik62acvRKx71sQ%2Bwqd%2BKb=auqb_NQkxT9F6y-Yr@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
=D0=92 Fri, 5 Nov 2010 13:39:05 -0400
Alejandro Imass <ait@p2ee.org> =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 <remko@elvandar.org>
> 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<stdio.h>
#include<stdlib.h>

#if defined(__ZTC__)
 #include <dos.h>
 #define FAR _far
 #define Fcalloc farcalloc
 #define Ffree farfree
 #define Size_T unsigned long
#elif defined(__TURBOC__)
 #include <alloc.h>
 #define FAR far
 #define Fcalloc farcalloc
 #define Ffree farfree
 #define Size_T unsigned long
#else /* assume MSC/QC */
 #include <malloc.h>
 #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<n)
      {
            if (++col =3D=3D 11)
            {
                  col =3D 1;
                  if (++col1 =3D=3D 6)
                  {
                        col1 =3D 0;
                        printf("\n");
                        printf("%4ld",m%10);
                  }
                  else  printf("%3ld",m%10);
            }
            else  printf("%ld",m);
            cnt++;
      }
}

void xprint(long m)
{
      long ii, wk, wk1;

      if (m < 8)
      {
            for (ii =3D 1; ii <=3D loc; )
                  yprint(stor[(int)(ii++)]);
            loc =3D 0;
      }
      else
      {
            if (m > 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 <number_of_digits>");
            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);
}
///////////////////////////////////////////////////////////////////////////=
///////////////////////////////////////////////////////////////////////////=
///



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20101105203702.1a364893>