From owner-freebsd-questions@FreeBSD.ORG Fri Nov 5 18:50:32 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 170AB106566B for ; Fri, 5 Nov 2010 18:50:32 +0000 (UTC) (envelope-from gnrp@physik.tu-berlin.de) Received: from mail.tu-berlin.de (mail.tu-berlin.de [130.149.7.33]) by mx1.freebsd.org (Postfix) with ESMTP id BB3208FC0A for ; Fri, 5 Nov 2010 18:50:30 +0000 (UTC) X-tubIT-Incoming-IP: 130.149.58.163 Received: from mail.physik-pool.tu-berlin.de ([130.149.58.163] helo=mail.physik.tu-berlin.de) by mail.tu-berlin.de (exim-4.69/mailfrontend-b) with esmtp for id 1PERMr-0003IB-95; Fri, 05 Nov 2010 19:50:30 +0100 Received: from localhost (localhost.physik-pool.tu-berlin.de [127.0.0.1]) by mail.physik.tu-berlin.de (Postfix) with ESMTP id 7366211402 for ; Fri, 5 Nov 2010 19:50:28 +0100 (CET) X-Virus-Scanned: amavisd-new at physik.tu-berlin.de Received: from mail.physik.tu-berlin.de ([127.0.0.1]) by localhost (mail.physik-pool.TU-Berlin.DE [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id mnxXzzyvfLHX for ; Fri, 5 Nov 2010 19:50:26 +0100 (CET) Received: from adolfputzen (laptopecke.physik-pool.tu-berlin.de [130.149.58.159]) (using TLSv1 with cipher DHE-RSA-AES128-SHA (128/128 bits)) (No client certificate requested) by mail.physik.tu-berlin.de (Postfix) with ESMTPSA id 23D4011401 for ; Fri, 5 Nov 2010 19:50:26 +0100 (CET) Date: Fri, 5 Nov 2010 19:50:19 +0100 From: Julian Fagir To: freebsd-questions@freebsd.org Message-ID: <20101105195019.4462c479@adolfputzen> In-Reply-To: References: X-Mailer: Claws Mail 3.7.6 (GTK+ 2.14.7; x86_64-unknown-linux-gnu) Mime-Version: 1.0 Content-Type: multipart/signed; boundary="Sig_/QJ_Ts1XzibFJAAaulhvm4P1"; protocol="application/pgp-signature"; micalg=PGP-SHA1 X-tubIT-Score: 0.0 () 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:50:32 -0000 --Sig_/QJ_Ts1XzibFJAAaulhvm4P1 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Hi, > Does anyone has a "generate-pi.c" source code? The solution of Ivan Klymenko is surely much more suffisticated, but as I wrote this down, I just want to publish it... ;-) 1 #include 2 #include 3 #include 4=20 5 // Change this for a more accurate result. 6 long max =3D 100000000; 7 double a, b; 8 double pi; 9 long counter; 10 long i; 11=20 12 int main() { 13 for (i =3D 0; i< max; i++) { 14 a =3D drand48(); 15 b =3D drand48(); 16 if (a*a + b*b <=3D 1) 17 counter++; 18 } =20 19 pi =3D 4*counter; 20 =20 21 printf("%e\n", pi); 22 return(0); 23 } =20 Note that the result must be shifted to the potence of the max-int. I didn't care for the problems with long-lengths now, but just dividing would not ha= ve done the job. Also, this implementations is stupid, as you see, no caring for the lengths of the variables in the computer, if you go too far with your max, you will surely become problems with the maximum number that can be represented. The detail of this approximation heavily depends on the pseudo-rng you are using, as does its correctness (e.g., when your 'rng' always returns 10, pi would be computed to be 10). But if you have a good prng, it can approximate pi to a fair amount of numbers. If you had *real* random numbers (whatever that might be), you could even be more approriate. This approximation is stupid, but I like the simplicity of it (we did it in uni last year). Just take 'random' numbers and look whether they are in a circle (that's the a*a + b*b <=3D 1). Regards, Julian --Sig_/QJ_Ts1XzibFJAAaulhvm4P1 Content-Type: application/pgp-signature; name=signature.asc Content-Disposition: attachment; filename=signature.asc -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) iEUEARECAAYFAkzUUewACgkQFV4nWcOPv/CtWACYwrAxjpbfDm0hbnskzxhN4SO+ IgCeI3KotjqE7fWPfZ32MKt0zES5Ero= =nlIO -----END PGP SIGNATURE----- --Sig_/QJ_Ts1XzibFJAAaulhvm4P1--