Re: Print value of p from the infinite series
- From: Lew <lew@xxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 27 Feb 2007 08:15:29 -0500
Leah wrote:
Now, I only thinking that is this the correct way to do it or not
accurate at all? Is it a sane way to change the expression to:
for (int i = 1; p != 3.141; i += 2)
some sort of that, but how to tell compiler only compare decimal
points as needed?
It depends on what you need, but for this type of algorithm the usual approach is to compare the difference between p and the target value to a small value epsilon, like this:
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.
- Lew
.
- Follow-Ups:
- 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 Stream Manipulation:
- Next by Date: Re: Print value of p from the infinite series
- 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
|