Re: how to generate pi in c
On 11 November 2010 12:06, Wojciech Puchar woj...@tensor.gdynia.pl wrote: Does anyone has a generate-pi.c source code? atanl(1) Er, arc tan of 1 is pi/4. Try atanl(1)*4, or for a less wasteful instruction try using the constant M_PI Also, forgive me if I'm wrong, but this looks like a homework question. Chris ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: how to generate pi in c
Does anyone has a generate-pi.c source code? atanl(1) ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: how to generate pi in c
Ian Smith smi...@nimnet.asn.au wrote: In freebsd-questions Digest, Vol 335, Issue 11, Message: 4 On Sat, 06 Nov 2010 01:00:34 -0700 per...@pluto.rain.com wrote: Julian Fagir g...@physik.tu-berlin.de wrote: Does anyone has a generate-pi.c source code? ... 1 #include stdlib.h 2 #include string.h 3 #include stdio.h 4 5 // Change this for a more accurate result. 6 long max = 1; 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 1), 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. 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 :) ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: how to generate pi in c
On Tue, 9 Nov 2010, per...@pluto.rain.com wrote: Ian Smith smi...@nimnet.asn.au wrote: In freebsd-questions Digest, Vol 335, Issue 11, Message: 4 On Sat, 06 Nov 2010 01:00:34 -0700 per...@pluto.rain.com wrote: Julian Fagir g...@physik.tu-berlin.de wrote: Does anyone has a generate-pi.c source code? ... 1 #include stdlib.h 2 #include string.h 3 #include stdio.h 4 5 // Change this for a more accurate result. 6 long max = 1; 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 1), 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. Ah. 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. due to truncation.) Ok, you can probably tell that I'm not really proficient in C; as an old Pascal programmer I'd have assumed an implied cast to pi's type, though I'd likely have specified 4.0 to make it clear. An approach more in keeping with the original would involve using sprintf, and then inserting the decimal point into the resulting string :) It's a bit O(N) to use for many digits .. still I like the approach. cheers, Ian ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: how to generate pi in c
Hi, just to get more off-topic... ;-) On Mon, 8 Nov 2010 20:01:19 +1100 (EST) Ian Smith smi...@nimnet.asn.au wrote: And while a square enclosing a circle, it's hardly squaring the circle: http://en.wikipedia.org/wiki/Squaring_the_circle .. but an interesting read nonetheless for unrequited seekers of pi-foo :) In our case, it is as possible/exact as computing pi. When computing pi, you resolve to the same problem as you have when 'squaring' a circle: Transcendental numbers over the given field. Just having rational numbers, you can just approximate pi, and as a human or computer, one doesn't have the power to imagine pi or give it an exact value. And I don't know, but doubt there's someone who can imagine anything else than rational or at least over Q algebraic numbers. The same with the squared circle: You can approximate it, but over the the field of the constructible numbers, the length is transcendental, so you cannot exactly draw it without further assumptions. Regards, Julian signature.asc Description: PGP signature
Re: how to generate pi in c
In freebsd-questions Digest, Vol 335, Issue 11, Message: 4 On Sat, 06 Nov 2010 01:00:34 -0700 per...@pluto.rain.com wrote: Julian Fagir g...@physik.tu-berlin.de wrote: Does anyone has a generate-pi.c source code? ... 1 #include stdlib.h 2 #include string.h 3 #include stdio.h 4 5 // Change this for a more accurate result. 6 long max = 1; 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 1), pi would already be 4 :) 20 21 printf(%e\n, pi); 22 return(0); 23 } ... This approximation is stupid ... Just take 'random' numbers and look whether they are in a circle (that's the a*a + b*b = 1). Not stupid, clever. Very clever. I rather doubt it resembles what the OP had in mind, but it is a brilliant example of what can be accomplished when one casts aside any perceived need to adopt a conventional approach. Agreed, quite elegant. Geometry being a bit rusty, I had to sketch it to really see it as simply the ratio of the area of the first quadrant of the unit circle (pi/4) to that of the unit square (1.0), times 4. The detail of this approximation heavily depends on the pseudo-rng you are using, as does its correctness Perhaps it would be useful in a PRNG test suite? (e.g., when your 'rng' always returns 10, pi would be computed to be 10) ... Bad example. If abs(drand48()) always exceeded (0.5 * sqrt(2.0)), a*a + b*b would always exceed 1.0, thus counter would never be incremented and pi would be reported as zero. That'd be one pretty sad PRNG :) but testing variance of the above algorithm's result to best known double pi is likely a useful test. And while a square enclosing a circle, it's hardly squaring the circle: http://en.wikipedia.org/wiki/Squaring_the_circle .. but an interesting read nonetheless for unrequited seekers of pi-foo :) cheers, Ian ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: how to generate pi in c
Julian Fagir g...@physik.tu-berlin.de wrote: Does anyone has a generate-pi.c source code? ... 1 #include stdlib.h 2 #include string.h 3 #include stdio.h 4 5 // Change this for a more accurate result. 6 long max = 1; 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; 20 21 printf(%e\n, pi); 22 return(0); 23 } ... This approximation is stupid ... Just take 'random' numbers and look whether they are in a circle (that's the a*a + b*b = 1). Not stupid, clever. Very clever. I rather doubt it resembles what the OP had in mind, but it is a brilliant example of what can be accomplished when one casts aside any perceived need to adopt a conventional approach. The detail of this approximation heavily depends on the pseudo-rng you are using, as does its correctness Perhaps it would be useful in a PRNG test suite? (e.g., when your 'rng' always returns 10, pi would be computed to be 10) ... Bad example. If abs(drand48()) always exceeded (0.5 * sqrt(2.0)), a*a + b*b would always exceed 1.0, thus counter would never be incremented and pi would be reported as zero. ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
how to generate pi in c
Does anyone has a generate-pi.c source code? Thanks.. :D :\ ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: how to generate pi in c
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 :\ -- /\ Best regards,| re...@freebsd.org \ / Remko Lodder | Xhttp://www.evilcoder.org/| Quis custodiet ipsos custodes / \ ASCII Ribbon Campaign| Against HTML Mail and News ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: how to generate pi in c
This is how I do it in perl use constant PI = 4 * atan2(1, 1); In C it owuld probably be (using math.h): pi = 4.0*atan(1.0); On Fri, Nov 5, 2010 at 1:15 PM, Remko Lodder re...@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 :\ -- /\ Best regards, | re...@freebsd.org \ / Remko Lodder | X http://www.evilcoder.org/ | Quis custodiet ipsos custodes / \ ASCII Ribbon Campaign | Against HTML Mail and News ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: how to generate pi in c
On Fri, 5 Nov 2010 13:39:05 -0400, Alejandro Imass a...@p2ee.org wrote: This is how I do it in perl use constant PI = 4 * atan2(1, 1); In C it owuld probably be (using math.h): pi = 4.0*atan(1.0); Or use M_PI from /usr/include/math.h, as we already #include'd it. :-) #define M_PI3.14159265358979323846 -- Polytropon Magdeburg, Germany Happy FreeBSD user since 4.0 Andra moi ennepe, Mousa, ... ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: how to generate pi in c
On Fri, Nov 5, 2010 at 2:13 PM, Polytropon free...@edvax.de wrote: On Fri, 5 Nov 2010 13:39:05 -0400, Alejandro Imass a...@p2ee.org wrote: This is how I do it in perl use constant PI = 4 * atan2(1, 1); In C it owuld probably be (using math.h): pi = 4.0*atan(1.0); Or use M_PI from /usr/include/math.h, as we already #include'd it. :-) #define M_PI 3.14159265358979323846 jaja. live and learn -- Polytropon Magdeburg, Germany Happy FreeBSD user since 4.0 Andra moi ennepe, Mousa, ... ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: how to generate pi in c
В Fri, 5 Nov 2010 13:39:05 -0400 Alejandro Imass a...@p2ee.org пишет: This is how I do it in perl use constant PI = 4 * atan2(1, 1); In C it owuld probably be (using math.h): pi = 4.0*atan(1.0); On Fri, Nov 5, 2010 at 1:15 PM, Remko Lodder re...@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 :\ -- /\ Best regards, | re...@freebsd.org \ / Remko Lodder | X http://www.evilcoder.org/ | Quis custodiet ipsos custodes / \ ASCII Ribbon Campaign | Against HTML Mail and News ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org adapt the code to build on FreeBSD... ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: how to generate pi in c
В Fri, 5 Nov 2010 13:39:05 -0400 Alejandro Imass a...@p2ee.org пишет: This is how I do it in perl use constant PI = 4 * atan2(1, 1); In C it owuld probably be (using math.h): pi = 4.0*atan(1.0); On Fri, Nov 5, 2010 at 1:15 PM, Remko Lodder re...@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 :\ -- /\ Best regards, | re...@freebsd.org \ / Remko Lodder | X http://www.evilcoder.org/ | Quis custodiet ipsos custodes / \ ASCII Ribbon Campaign | Against HTML Mail and News ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org 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 */ #includestdio.h #includestdlib.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 = ((*l2) 0 ? (*l2) / lmod: -(-(*l2) / lmod) - 1); *l2 -= k * lmod; *l1 += k * lp; } void yprint(long m) { if (cntn) { if (++col == 11) { col = 1; if (++col1 == 6) { col1 = 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 = 1; ii = loc; ) yprint(stor[(int)(ii++)]); loc = 0; } else { if (m 9) { wk = m / 10; m %= 10; for (wk1 = loc; wk1 = 1; wk1--) { wk += stor[(int)wk1]; stor[(int)wk1] = wk % 10; wk /= 10; } } } stor[(int)(++loc)] = m; } void memerr(int errno) { printf(\a\nOut of memory error #%d\n, errno); if (2 == errno) Ffree(mf); _exit(2); } int main(int argc, char *argv[]) { int i=0; char *endp; stor[i++] = 0; if (argc 2) { puts(\aUsage: PI number_of_digits); return(1); } n = strtol(argv[1], endp, 10); if (NULL == (mf = Fcalloc((Size_T)(n + 3L), (Size_T)sizeof(long memerr(1); if (NULL == (ms = Fcalloc((Size_T)(n + 3L), (Size_T)sizeof(long memerr(2); printf(\nApproximation of PI to %ld digits\n, (long)n); cnt = 0; kf = 25; ks = 57121L; mf[1] = 1L; for (i = 2; i = (int)n; i += 2) { mf[i] = -16L; mf[i+1] = 16L; } for (i = 1; i = (int)n; i += 2) { ms[i] = -4L; ms[i+1] = 4L; } printf(\n 3.); while (cnt n) { for (i = 0; ++i = (int)n - (int)cnt; ) { mf[i] *= 10L; ms[i] *= 10L; } for (i =(int)(n - cnt + 1); --i = 2; ) { temp = 2 * i - 1; shift(mf[i - 1], mf[i], temp - 2, temp * kf); shift(ms[i - 1], ms[i], temp - 2, temp * ks); } nd = 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); }
Re: how to generate pi in c
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 stdlib.h 2 #include string.h 3 #include stdio.h 4 5 // Change this for a more accurate result. 6 long max = 1; 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; 20 21 printf(%e\n, pi); 22 return(0); 23 } 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 have 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 = 1). Regards, Julian signature.asc Description: PGP signature
Re: how to generate pi in c
On Fri, Nov 05, 2010 at 05:40:39PM +0100, Arthur Bela wrote: Does anyone has a generate-pi.c source code? Search for pi spigot algorithm. Here is a tiny C program from Jeremy Gibbon's Unbounded Spigot paper (due to Dik Winter and Achim Flammenkamp): a[52514],b,c=52514,d,e,f=1e4,g,h;main(){for(;b=c-=14;h=printf(%04d, e+d/f))for(e=d%=f;g=--b*2;d/=g)d=d*b+f*(h?a[b]:f/5),a[b]=d%--g;} This produces the first 15,000 digits concisely, but is obfuscated. If you need an unbounded number of digits, search out the spigot algorithm papers. Alex ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org