algorithm for finding Pi in C
Collapse
This topic is closed.
X
X
-
aarklon@gmail.comTags: None -
pete
Re: algorithm for finding Pi in C
aarklon@gmail.c om wrote:This works for me:
#include <float.h>
/*
** pi == (atan(1.0 / 3) + atan(1.0 / 2)) * 4
*/
static double fs_pi(void);
static double fs_pi(void)
{
unsigned n;
double a, b;
static double p;
static int initialized;
if (!initialized) {
initialized = 1;
n = 1;
a = 3;
do {
a /= 9;
b = a / n;
n += 2;
a /= 9;
b -= a / n;
n += 2;
p += b;
} while (b DBL_EPSILON / 4);
n = 1;
a = 2;
do {
a /= 4;
b = a / n;
n += 2;
a /= 4;
b -= a / n;
n += 2;
p += b;
} while (b DBL_EPSILON / 2);
p *= 4;
}
return p;
}
--
pete
-
Richard Heathfield
Re: algorithm for finding Pi in C
aarklon@gmail.c om said:
Presumably you want Pi? Easy. It's about 3.
--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Comment
-
spinoza1111
Re: algorithm for finding Pi in C
On May 24, 2:44 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:So we doan' need no arbitrary "unlimited" "limited only by time andaark...@gmail.c om said:
>>Hi all,>see:-http://mathforum.org/library/drmath/view/54456.html
Presumably you want Pi? Easy. It's about 3.
memory space" precision?
Wouldn't 3.14 be a better answer in all cases? That's what most
"normal geeks" remember from math class.
And isn't usually a #define const?
>
--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999Comment
-
Richard Heathfield
Re: algorithm for finding Pi in C
spinoza1111 said:
It depends. Sometimes we do, and sometimes we don't.On May 24, 2:44 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:>>aark...@gmail. com said:
>>>>Hi all,>>see:-http://mathforum.org/library/drmath/view/54456.html
>Presumably you want Pi? Easy. It's about 3.
So we doan' need no arbitrary "unlimited" "limited only by time and
memory space" precision?
No, not in all cases. In some cases, "about 3" is far superior, althoughWouldn't 3.14 be a better answer in all cases?
admittedly "just over 3" is even better. Case in point: you're parked by
the lake, looking at a map thereof. Using your thumb against the map scale
indicator and then against the lake, you can see that it's about a mile
across, and roughly circularish. There's a path all the way round. In this
kind of terrain (reasonably flat, for obvious reasons) you can manage,
say, 4mph. Your time, however, is not unlimited. Have you got time to walk
around the lake? In such a situation, taking pi as "three-and-a-bit" is
far more appropriate than the more pernickety 3.14.
Note that, as an estimate of pi, 3 is only about 4.5+% short. That's not
bad for a single digit.
No (but somehow I get the feeling that either I'm misinterpreting yourAnd isn't usually a #define const?
question, or you're going to misinterpret my answer, or perhaps both).
--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Comment
-
MisterE
Re: algorithm for finding Pi in C
Wow it gives all the worst ways to calculate pi, and it has nothing specific
to C. Its win-win!
Comment
-
nembo kid -
santosh
Re: algorithm for finding Pi in C
nembo kid wrote:
That's to prevent the objects from having external linkage, which ispete ha scritto:
>>>static d
Why all these 'static'?
mainly to control namespace pollution. IOW these identifiers are
visible only in this translation unit, from where they are defined
until the end of the unit, so you can reuse these identifiers for other
purposes elsewhere. The default linkage for functions and file scope
objects is external. The static qualifier in these cases suppress this.
Comment
-
pete
Re: algorithm for finding Pi in C
nembo kid wrote:There is no reason to calculate that one,pete ha scritto:
>>>static d
Why all these 'static'?
more than once.
--
pete
Comment
-
Jack Klein
Re: algorithm for finding Pi in C
On Fri, 23 May 2008 20:53:16 -0700 (PDT), aarklon@gmail.c om wrote in
comp.lang.c:
Even better:
double pi = acos(-1);
Of course, if you want it outside of a function, you can't initialize
it with a function, so instead:
double pi;
int main(void)
{
pi = acos(-1);
/* stuff */
return 0;
}
Don't forget to include <math.h>, and you might have to do some extra
work in linking if you work on an antique system.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
Comment
-
Andy G.
Re: algorithm for finding Pi in C
On Sat, 24 May 2008 22:31:55 -0500, Jack Klein wrote:
Or take it a step further and copy and paste the output of the followingOn Fri, 23 May 2008 20:53:16 -0700 (PDT), aarklon@gmail.c om wrote in
comp.lang.c:
>>
Even better:
>
double pi = acos(-1);
>
Of course, if you want it outside of a function, you can't initialize it
with a function, so instead:
>
double pi;
>
int main(void)
{
pi = acos(-1);
/* stuff */
return 0;
}
program into a suitable header file.
/* pi.c */
#include <math.h>
#include <stdio.h>
int main(void)
{
printf("#define PI %.32f\n", acos(-1.0));
return 0;
}
Output:
#define PI 3.1415926535897 931159979634685 4419
s/antique/linuxDon't forget to include <math.h>, and you might have to do some extra
work in linking if you work on an antique system.
jaysome@ubuntu:/tmp$ gcc -o pi pi.c
/tmp/ccIx5hjS.o: In function `main':
pi.c:(.text+0x1 c): undefined reference to `acos'
collect2: ld returned 1 exit status
Of course the way to fix this is explained in the C FAQ:
jaysome@ubuntu:/tmp$ gcc -o pi pi.c -lm
Regards
--
jay
Comment
-
pete
Re: algorithm for finding Pi in C
Andy G. wrote:That's wrong.On Sat, 24 May 2008 22:31:55 -0500, Jack Klein wrote:
>>>On Fri, 23 May 2008 20:53:16 -0700 (PDT), aarklon@gmail.c om wrote in
>comp.lang.c:
>>>Even better:
>>
> double pi = acos(-1);
>>
>Of course, if you want it outside of a function, you can't initialize it
>with a function, so instead:
>>
>double pi;
>>
>int main(void)
>{
> pi = acos(-1);
> /* stuff */
> return 0;
>}
Or take it a step further and copy and paste the output of the following
program into a suitable header file.
>
/* pi.c */
#include <math.h>
#include <stdio.h>
int main(void)
{
printf("#define PI %.32f\n", acos(-1.0));
return 0;
}
>
Output:
>
#define PI 3.1415926535897 931159979634685 4419
Pi is 3.1415926535897 932384626433832 7950
s/antique/linux
>
jaysome@ubuntu:/tmp$ gcc -o pi pi.c
/tmp/ccIx5hjS.o: In function `main':
pi.c:(.text+0x1 c): undefined reference to `acos'
collect2: ld returned 1 exit status
>
Of course the way to fix this is explained in the C FAQ:
>
jaysome@ubuntu:/tmp$ gcc -o pi pi.c -lm
>
Regards
--
pete
Comment
-
Keith Thompson
Re: algorithm for finding Pi in C
"Andy G." <andyg447@spamc op.netwrites:
[...]#include <float.h>Or take it a step further and copy and paste the output of the following
program into a suitable header file.
>
/* pi.c */
#include <math.h>
#include <stdio.h>
int main(void)
{
printf("#define PI %.32f\n", acos(-1.0));
return 0;
}
>
Output:
>
#define PI 3.1415926535897 931159979634685 4419
#define PI 3.1415926535897 923284626433832 795028841971693 993751058209749 44592
#if LDBL_DIG 64
#error "Need more digits for PI"
#endif
Or you can write code to compute it for you if you're concerned that
the value might change.
--
Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Comment
-
Richard Heathfield
Re: algorithm for finding Pi in C
Keith Thompson said:
<snip>"Andy G." <andyg447@spamc op.netwrites:
His program's first error is in the 17th digit (the 16th decimal place).> printf("#define PI %.32f\n", acos(-1.0));
> return 0;
>}
>>
>Output:
>>
>#define PI 3.1415926535897 931159979634685 4419
Your first error, however, is in the 16th digit (the 15th decimal place).#include <float.h>
#define PI
#3.141592653589 792328462643383 279502884197169 399375105820974 944592
:-p
--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Comment
-
Keith Thompson
Re: algorithm for finding Pi in C
Richard Heathfield <rjh@see.sig.in validwrites:Whoops. s/232/323/Keith Thompson said:
>>>"Andy G." <andyg447@spamc op.netwrites:
<snip>
>>>> printf("#define PI %.32f\n", acos(-1.0));
>> return 0;
>>}
>>>
>>Output:
>>>
>>#define PI 3.1415926535897 931159979634685 4419
His program's first error is in the 17th digit (the 16th decimal place).
>>>#include <float.h>
>#define PI
>#3.14159265358 979232846264338 327950288419716 939937510582097 4944592
Your first error, however, is in the 16th digit (the 15th decimal place).
:-p
(His error was due to rounding; mine was a typo.)
--
Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Comment
Comment