Re: Print value of p from the infinite series
- From: Thomas Fritsch <i.dont.like.spam@xxxxxxxxxxx>
- Date: Tue, 27 Feb 2007 17:09:23 GMT
Leah wrote:
Nothing wrong with the constant value, but with constant name.final double four = 4.0;
Why did you do this?
constants value, anything wrong with this?
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.
Here it is easy to find a good self-explanatory name better than "flag". Idouble p = 0.0;
int count = 0;
boolean flag = true;
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
.
- 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
- Print value of p from the infinite series
- Prev by Date: Re: Print value of p from the infinite series
- Next by Date: Java Applet Notinited
- 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):