Java Help

Hexus0

Gawd
Joined
Feb 28, 2005
Messages
590
I'm hoping I'm not violating any of the forum rules, I have put some work into this code, but I'm sort of stuck on some exact things lol...

I'm working on a homework assignment and one part of the assignment is to write a method to approximate the value of Pi using the series 4(1 - 1/3 + 1/5 - 1/7 + 1/9 + 1/11 ... - 1/(2i-1) + 1/(2i+1), assuming i is 1000000.
This is my code:
Code:
	public static double pi()
	{
		double pi = 0.0;
		
		for(int i = 0; i <= 1000000; i++)
		{
		 	pi += 4 * (1.0/(1 + i*2) * ((i % 2 == 0) ? 1 : -1));
		}
	
		return pi;
	}
and my return is 3.1415936535887745 when the return needs to be 3.141590653589692, the first 7 digits are right, but beyond its off. I've been recommended to extend the value of i, but the assignment says to assume i is 1000000, so I'm going to assume I can't make i and bigger, any ideas?

Another problem I'm having is approximating the square root of a number (like 10 for example). My professor wants us to write a method to approximate the square root of a value by using the formula nextGuess = (lastGuess + (num / lastGuess)) / 2 and when the difference between nextGuess and lastGuess is less then .00001 you can assume that nextGuess is the approximated square root.

Edit: Fixed my square root problem.

Thanks and sorry about the long post.
 
for your nextGuess problem you are assigning
Code:
lastGuess = nextGuess

which will make your while statement true all the time.

try this.

Code:
nextGuess = 1;
do{
lastGuess <- nextGuess;
nextGuess <- (lastGuess + (num/lastGuess))/2;
}while(abs((nextGuess - lastGuess)) > .0001);
return nextGuess;
 
Seems like a goofy requirement that your answer should be exactly some pre-determined approximation of pi. As long as your algorithm converges to the true value, who cares? For shits and giggles, I tried:
Code:
        for (int i = 1; i <= 1000000; i += 2) {
            pi +=  4 / (2.0*i - 1) - 4 / (2.0*i + 1);
        }
but that gives 3.1415916535899084: not what you're looking for.
 
...try using < instead of <=

FYI : (-1/(2i-1) + 1/(2i+1)) isn't what you're calculating...
 
FYI : (-1/(2i-1) + 1/(2i+1)) isn't what you're calculating...
This struck me as odd at first as well...

But it's a full expansion of the i-th partial sum, so the last two terms (for odd i) are
... - 1/(2(i-1)+1) + 1/(2i+1)
= ... - 1/(2i-1) + 1/(2i+1)


...my return is 3.1415936535887745 when the return needs to be 3.141590653589692, the first 7 digits are right, but beyond its off.
Can't figure this one out for the life of me...

I get exactly the same answer as you for 0 <= i <= 1000000, and 3.14159165... for 0 <= i< 1000000, and with more terms your approximation is going to fall between these two, so a higher value of i won't get you the number you're after.

My first thought was rounding error, but I did everything I could to minimise it, and the difference (a mere 2e-14) doesn't even come close to explaining it.

If you set your loop condition to i<1000000 instead of i<=1000000, you'll see that your program agrees with everyone else here. So at this point, I'd be asking your teacher to check his code...
 
TBH I agree with you guys completely. I tried re-writing the code a dozen times to figure out why it wasn't the exact figure he wants us to get. I thought I might have over-looked something and you guys might of spotted it out, but that wasn't the case. I'm just going to turn in the assignment with what I have and if I get marked down I'll ask him for his reasoning. Thanks for the help and quick responses! :D
 
Back
Top