Re: Print value of p from the infinite series



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?

.



Relevant Pages

  • RE: Robby, __int8 limited to: -128 to 127.
    ... And yes an 8bit int cannot hold a value of 256. ... //MIDSB OF MESSAGE ADDRESS IN PAR FLASH ... In order to access the data from my external memory I need a three byte ... and increment the A0 value from 1 to 2. ...
    (microsoft.public.vc.language)
  • Writing Classes
    ... Clock application ... private int hr; //store hours ... public void setTime{ ... //Method to increment the time by one second ...
    (comp.lang.java.programmer)
  • Re: Definition of expression and statement.
    ... The fact that a while loop is recognized ... behavior, e.g., if a is an "int" variable and is initially set to ... The various modifier operators, ... increment and decrement, have TWO uses: ...
    (comp.lang.c)
  • Re: Question about ++
    ... I have got a question about the post increment operator. ... It seems int he code above the ++ is completly disregarded ... Java completely evaluates the right hand side of an assignment before ... Do the assignment, setting i to the right hand side result, 1. ...
    (comp.lang.java.programmer)
  • Re: Pointers gone mad!
    ... Yes, its true Igor, I didn't see it with those values. ... int INCBY, //AMOUNT OF INCREMENTATIONS REQUIRED ... //MIDSB OF MESSAGE ADDRESS IN PAR FLASH ... if {// previous increment overflowed ...
    (microsoft.public.vc.language)