Re: Print value of p from the infinite series



Leah wrote:

final double four = 4.0;

Why did you do this?

constants value, anything wrong with this?
Nothing wrong with the constant value, but with constant name.
I suggest a name like
final double MAX_VALUE = 4.0;
final double NUMBER_OF_ELEPHANTS = 4.0;
Of course these names are wrong. Actually I cannot come up with a good
self-explanatory name, because I don't understand the meaning of your
constant "four". So you have to invent a good name.

double p = 0.0;
int count = 0;
boolean flag = true;
Here it is easy to find a good self-explanatory name better than "flag". I
suggest
boolean incrementing = true;
Then it is immediately obvious, that incrementing=true means your variable p
is incrementing, and incrementing=false means it is decrementing.


System.out.printf("%s%11s%14s%14s%13s\n", "full:", "p:", "flag:",
"p in int:", "counter:");

for (int i = 1; (int)(p * 1000) != 3141; i += 2) {
++count;
p = (flag) ? (p + (four/i) ) : ( p - (four/i));
System.out.printf("%f %10.3f %9b %13d %12d\n", p, p, flag,
(int)(p * 1000), count);
flag = !flag; // switch back and forth
}

--
Thomas
.