From owner-freebsd-questions@FreeBSD.ORG Wed Nov 10 04:10:39 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 CF224106566B for ; Wed, 10 Nov 2010 04:10:39 +0000 (UTC) (envelope-from perryh@pluto.rain.com) Received: from agora.rdrop.com (agora.rdrop.com [199.26.172.34]) by mx1.freebsd.org (Postfix) with ESMTP id C157A8FC18 for ; Wed, 10 Nov 2010 04:10:38 +0000 (UTC) Received: from agora.rdrop.com (66@localhost [127.0.0.1]) by agora.rdrop.com (8.13.1/8.12.7) with ESMTP id oA99BwNC039580 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT); Tue, 9 Nov 2010 01:11:59 -0800 (PST) (envelope-from perryh@pluto.rain.com) Received: (from uucp@localhost) by agora.rdrop.com (8.13.1/8.12.9/Submit) with UUCP id oA99Bw61039579; Tue, 9 Nov 2010 01:11:58 -0800 (PST) Received: from fbsd61 by pluto.rain.com (4.1/SMI-4.1-pluto-M2060407) id AA00679; Tue, 9 Nov 10 01:09:53 PST Date: Tue, 09 Nov 2010 01:09:46 -0800 From: perryh@pluto.rain.com To: smithi@nimnet.asn.au Message-Id: <4cd90fda.M0ZCOlvxcAzTnwuw%perryh@pluto.rain.com> References: <20101106120033.CB14610656D7@hub.freebsd.org> <20101108182717.M66572@sola.nimnet.asn.au> In-Reply-To: <20101108182717.M66572@sola.nimnet.asn.au> User-Agent: nail 11.25 7/29/05 Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Cc: freebsd-questions@freebsd.org, gnrp@physik.tu-berlin.de 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: Wed, 10 Nov 2010 04:10:39 -0000 Ian Smith wrote: > In freebsd-questions Digest, Vol 335, Issue 11, Message: 4 > On Sat, 06 Nov 2010 01:00:34 -0700 perryh@pluto.rain.com wrote: > > Julian Fagir wrote: > > > > Does anyone has a "generate-pi.c" source code? > > ... > > > 1 #include > > > 2 #include > > > 3 #include > > > 4 > > > 5 // Change this for a more accurate result. > > > 6 long max = 100000000; > > > 7 double a, b; > > > 8 double pi; > > > 9 long counter; > > > 10 long i; > > > 11 > > > 12 int main() { > > > 13 for (i = 0; i< max; i++) { > > > 14 a = drand48(); > > > 15 b = drand48(); > > > 16 if (a*a + b*b <= 1) > > > 17 counter++; > > > 18 } > > > 19 pi = 4*counter; > > Surely that should be 'pi = 4 * counter / max;' otherwise even if the > integer counter were only 1 (of 100000000), pi would already be 4 :) The part I snipped out included a note that it was only generating the digits, not trying to show the decimal point placed properly. With that understanding, and as long as max is a (large-ish) power of 10, the division is not needed. (If the division were to be inserted, at least one of its operands would need to be cast to double, or pi would likely be reported as 3.0000 due to truncation.) An approach more in keeping with the original would involve using sprintf, and then inserting the decimal point into the resulting string :)