Hey everyone, I'm writing a program for my intro to Java class, and I'm having a couple of problems.
One part of the program was to write a method to estimate the value of Pi using the following series: 4 * (1 - 1/3 + 1/5 - 1/7 + 1/9 ... - 1/(2i-1) + 1(2i+1)
Heres the method I wrote:
For Pi it returns: 3.1415936535887745 when my professor wants it to return: 3.141590653589692, for the first 7 digits its right, but after that I'm getting something else.
My next problem is creating a method that approximates the square root of a number by repeating the following formula nextGuess = (lastGuess + (num / lastGuess) / 2. The prompt states that when nextGuess and lastGuess are almost identical, nextGuess is the approximate square root. lastGuess is given, as 1.0 and it states that when the difference between nextGuess and lastGuess is less than a very small number (.00001) you can claim that nextGuess is the approximated square root of num.
Heres the method I wrote:
The problem is the loop doesn't end, it just repeats. He wants us to use the input of 10 and it should return 3.16227660168379, which it does when you print out nextGuess, so I can correctly get the square root, but I can't stop the loop. I've printed out nextGuess - lastGuess to see what it is and it prints out 0.0, which should essential stop the loop (0.0 <= .00001), unless there's a positive number less then 0 that Java knows that I don't know..
Thanks for the help everyone, sorry about the long post.
One part of the program was to write a method to estimate the value of Pi using the following series: 4 * (1 - 1/3 + 1/5 - 1/7 + 1/9 ... - 1/(2i-1) + 1(2i+1)
Heres the method I wrote:
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;
}
For Pi it returns: 3.1415936535887745 when my professor wants it to return: 3.141590653589692, for the first 7 digits its right, but after that I'm getting something else.
My next problem is creating a method that approximates the square root of a number by repeating the following formula nextGuess = (lastGuess + (num / lastGuess) / 2. The prompt states that when nextGuess and lastGuess are almost identical, nextGuess is the approximate square root. lastGuess is given, as 1.0 and it states that when the difference between nextGuess and lastGuess is less than a very small number (.00001) you can claim that nextGuess is the approximated square root of num.
Heres the method I wrote:
Code:
public static double sqrt(double num)
{
double nextGuess, lastGuess;
lastGuess = 1.0;
nextGuess = 0;
do{
nextGuess = (lastGuess + (num / lastGuess)) / 2.0;
lastGuess = nextGuess;
}
while((nextGuess - lastGuess) <= 0.00001);
return nextGuess;
}
The problem is the loop doesn't end, it just repeats. He wants us to use the input of 10 and it should return 3.16227660168379, which it does when you print out nextGuess, so I can correctly get the square root, but I can't stop the loop. I've printed out nextGuess - lastGuess to see what it is and it prints out 0.0, which should essential stop the loop (0.0 <= .00001), unless there's a positive number less then 0 that Java knows that I don't know..
Thanks for the help everyone, sorry about the long post.