programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Understanding small program

Ranch Hand
Posts: 82
Hello every one i need to help me in understanding this small program
...you can approximate by using the following series
∏=4(1-1/3+1/5-1/7+1/9-1/11+1/13-.....-1/(2i-1)+1/(2i+1))
write a program that displays the value for i=10000,20000,....,and 100000
I've developed this code to solve the problem

but i don't know how can i compute the value when i is equal to 10000,20000,.....,and 100000
note that the output of my program is
The value of p is 3.1415926517266946

Sheriff
Posts: 3015
12
Do you mean that double doesn't have enough precision to handle the larger values of i? In that case, you would have to use java.math.BigDecimal

Roger Fed
Ranch Hand
Posts: 82

Do you mean that double doesn't have enough precision to handle the larger values of i

no this isn't my question
is this code I've written solve the problem?
and what about "compute the value when i is equal to 10000,20000,.....,and 100000"
this is supposed to be implemented in my code

Greg Charles
Sheriff
Posts: 3015
12
Ah, got it. The second part of your for statement should be the limit. Comparing i to a value computed from i isn't a good idea. For any non-negative value of i, i < 2 * i + 1 will always be true. You want something more like:

That's not exactly right though, because your i isn't being used exactly the same way it's used in the formula. I'd advise changing the code so it matches the formula more closely and see if you can dispense with the count variable entirely. Try a low limit of i and put in some print statements at first to help you get the code right.

Roger Fed
Ranch Hand
Posts: 82

I'd advise changing the code so it matches the formula more closely

I take your advice and I've improved my code to the following

here I've used nested loops..........the outer to control the value of i
and the inner to control the value of x(denominator) and gave me the result i really want.