Implement the sqrt method using an approximate approach. Can't exit the loop even if the condition is false
I'm so close to finishing my practice question, just stuck, not sure why I can't get out of the loop after getting the correct result.
Inquire about implementing the sqrt method using an approximate approach.
- Let be
num
the number to apply the sqrt method. - For
num> 1
,lowerLimit = 1
andupperLimit = number
- Then find
midpoint
lowerLimit
andupperLimit
which(lowerLimit+upperLimit)/2
- Square
midpoint
,squareMidpoint = Math.pow(midpoint, 2)
- If
squareMidpoint > num
,upperLimit = midpoint
, the elselowerLimit= midpoint
. - Repeat the third step one more time until you get num with 8 significant digits.
From step 1 to step 5, I think I did the right thing, since the result is correct.
I don't really understand step 6 well.
My problem is num = 4
, the program keeps the print 2
forever.
Here is my code:
import java.lang.Math;
public class p2q4 {
public static void main(String[] args) {
//Implement the sqrt method using the approximation approach
//initialized lowerLimit and upperLimit solve an error.
double lowerLimit = 0, upperLimit = 0;
//num is the number to square root.
double num = 5;
//For number greater than one,
if (num > 1) {
lowerLimit = 1; //lower limit to one
upperLimit = num; //upper limit to the number
}
double squareMidpoint;
double midpoint;
do {
//Determine the midpoint between the lower and upper limits
midpoint = (lowerLimit + upperLimit) / 2;
//Evaluate the square of the midpoint
squareMidpoint = Math.pow(midpoint, 2);
//If the square of the midpoint is greater than the number
if (squareMidpoint > num) {
//upper limit to the midpoint
upperLimit = midpoint;
} else {
//lower limit to the midpoint
lowerLimit = midpoint;
}
//for debugging purpose
System.out.printf("midpoint=%f squareMidpoint=%f upperLimit=%f lowerLimit=%f upperLimit/lowerLimit=%f\n", midpoint, squareMidpoint, upperLimit, lowerLimit, upperLimit/lowerLimit);
//even though upperLimit/lowerLimit is '1' but still keep looping
} while (upperLimit/lowerLimit != 1); //I not sure this condition is correct.
//Output
System.out.printf("x = %.0f, root = %f\n", num, midpoint);
}
}
Here's my practical question:
Instead of using the sqrt method in the Math class, you were asked to implement the sqrt method using the approximate approach below:
For numbers greater than one, the square root method must initially set the lower limit to one and the upper limit to the number (since the square root of a number is always between one and the number).
Then he has to determine the midpoint between the lower and upper limits and estimate the square of the middle. If the square of the midpoint is less than a number, the square root method must move the upper limit to the midpoint and similarly, if the square of the midpoint is equal to a number, it should move the lower limit to the middle.
After moving the appropriate limit, the square root method should estimate the new midpoint and repeat the process until accuracy is obtained.
The required precision for double-precision floating point numbers is 8 significant digits. The accuracy at any iteration can be determined by dividing the difference between the limits by the lower limit.
If it is less than 1/108, any number between the limits will estimate the square root of the number with the required precision. To minimize error, the square root method should return the midpoint between finite limits that satisfy the precision requirement.
The square root method must return exact values ββfor special cases between zero and one.
If the application tries to calculate the square root of a negative number, the square root method should display an appropriate message and exit the program.
Any help is appreciated!
source to share
If you print upperLimit/lowerLimit
with more significant numbers, you will see that it gets smaller 1.0000000000000002
but never reaches 1, so your loop never ends.
Instead of staying in a loop while:
upperLimit/lowerLimit != 1
you must change the condition to:
while (upperLimit - lowerLimit > 0.0000000001)
which will exit the loop when the limits are close enough to each other.
What in step # 6 means "8 significant digits" - your approximation should correct the first 8 significant digits correctly.
source to share