Re: Print value of p from the infinite series
- From: "Leah" <Lea0x.Diz@xxxxxxxxx>
- Date: 28 Feb 2007 09:13:10 -0800
On Feb 27, 9:17 pm, Lew <l...@xxxxxxxxxxxxxxxxxxxx> wrote:
Lew wrote:
while ( (p - Math.PI) < 1E-6 )
or
for ( int i = 1; (p - Math.PI) < 1E-6; i += 2 )
Use your own values for the target and epsilon.
Exuse me, that condition should be inverted
while ( (p - Math.PI) > 1E-6 )
- Lew
Well, after tried a couple of changes, it doesn't works as expected.
Maybe I mis-understood the key.
Anyway, here is what come after the changes:
Still, it is near but not accurate:
<code>
real numbers: p: counter: > 1E-3
4.000000000000000 4.000 1 0.858407346410207
2.666666666666667 2.667 2 -0.474925986923126
3.466666666666667 3.467 3 0.325074013076874
....
3.140590649833284 3.141 998 -0.001002003756509
3.142593654340044 3.143 999 0.001001000750251
3.140592653839794 3.141 1000 -0.000999999749999
3.142591654339544 3.143 1001 0.000999000749751
</code>
The loops terminate at 1001, since 3.142591654339544 - Math.PI =
0.00099...
So, if I were to count the terms for only 3.141 :
<code>
double p = 0.0;
boolean increment = true;
System.out.printf("%s%12s%17s%12s\n", "real numbers:", "p:",
"counter:",
"> 1E-3");
int count = 0;
int i = 1;
do {
p = (increment) ? (p + (4.0 / i)) : (p - (4.0 / i));
System.out.printf("%.15f %10.3f %12d % 24.15f\n",
p, p, ++count, p - Math.PI);
increment = !increment;
i += 2;
} while ((p - Math.PI) > 1E-3 || (p - Math.PI) < 0);
</code>
Any ideas?
.
- References:
- Print value of p from the infinite series
- From: Lea0x . Diz
- Re: Print value of p from the infinite series
- From: Lew
- Re: Print value of p from the infinite series
- From: Leah
- Re: Print value of p from the infinite series
- From: Lew
- Re: Print value of p from the infinite series
- From: Lew
- Print value of p from the infinite series
- Prev by Date: Re: DST changes and tzupdater
- Next by Date: System property user.dir
- Previous by thread: Re: Print value of p from the infinite series
- Next by thread: Re: Print value of p from the infinite series
- Index(es):
Relevant Pages
|
|