使用近似方法实现 sqrt 方法。即使条件为假也无法退出循环
Implement sqrt method using the approximation approach. Cannot exit loop even the condition is false
我快要完成我的实际问题了,只是卡在不知道为什么我在得到正确结果后无法退出循环。
问题要求使用近似方法实现 sqrt 方法。
- 令
num
为应用sqrt方法的数字。
- 对于
num> 1
、lowerLimit = 1
和 upperLimit = number
- 然后求
lowerLimit
和upperLimit
的midpoint
即(lowerLimit+upperLimit)/2
midpoint
、squareMidpoint = Math.pow(midpoint, 2)
的平方
- 如果
squareMidpoint > num
,upperLimit = midpoint
,否则lowerLimit= midpoint
。
- 再次重复第三步,直到得到有效数字为8位的num。
从步骤 1 到步骤 5,我认为我做对了,因为输出是正确的。
其实我对第6步不是很了解
我的问题是如果 num = 4
,程序会一直打印 2
。
这是我的代码:
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
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);
}
}
这是我的实际问题:
Instead of using the sqrt method in the Math class, you have been
asked to implement the sqrt method using the approximation approach
described below:
For numbers greater than one, the square root method must initially
set a lower limit to one and an upper limit to the number (since the
square root of the number always lies between one and the number).
It must then determine the midpoint between the lower and upper limits
and evaluate the square of the midpoint. If the square of the midpoint
is greater than the number, the square root method must move the upper
limit to the midpoint and similarly if the square of the midpoint is
less than the number, it must move the lower limit to the midpoint.
After moving the appropriate limit, the square root method must
evaluate a new midpoint and repeat the process until the desired
precision is obtained.
The required precision for double precision floating point numbers is
8 significant digits. The precision at any iteration can be determined
by dividing the difference between the limits by the lower limit.
When this is less than 1/108 any number between the limits will be an
estimate of the square root of the number to the required precision.
To minimize the error, the square root method should return the
midpoint between the final limits that satisfy the precision
requirement.
The square root method must return exact values for the special cases
of zero and one.
If an application attempts to calculate the square root of a negative
number, the square root method should display an appropriate message
and terminate the program.
感谢任何帮助!
如果你用更多有效数字打印 upperLimit/lowerLimit
,你会看到它变得和 1.0000000000000002
一样小,但永远不会达到 1,这就是你的循环永远不会结束的原因。
而不是停留在循环中只要:
upperLimit/lowerLimit != 1
您应该将条件更改为:
while (upperLimit - lowerLimit > 0.0000000001)
这将在限制彼此足够接近时退出循环。
这就是第 6 步中“8 位有效数字”的含义 - 您的近似值应该使前 8 位有效数字正确。
我快要完成我的实际问题了,只是卡在不知道为什么我在得到正确结果后无法退出循环。
问题要求使用近似方法实现 sqrt 方法。
- 令
num
为应用sqrt方法的数字。 - 对于
num> 1
、lowerLimit = 1
和upperLimit = number
- 然后求
lowerLimit
和upperLimit
的midpoint
即(lowerLimit+upperLimit)/2
midpoint
、squareMidpoint = Math.pow(midpoint, 2)
的平方
- 如果
squareMidpoint > num
,upperLimit = midpoint
,否则lowerLimit= midpoint
。 - 再次重复第三步,直到得到有效数字为8位的num。
从步骤 1 到步骤 5,我认为我做对了,因为输出是正确的。
其实我对第6步不是很了解
我的问题是如果 num = 4
,程序会一直打印 2
。
这是我的代码:
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
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);
}
}
这是我的实际问题:
Instead of using the sqrt method in the Math class, you have been asked to implement the sqrt method using the approximation approach described below:
For numbers greater than one, the square root method must initially set a lower limit to one and an upper limit to the number (since the square root of the number always lies between one and the number).
It must then determine the midpoint between the lower and upper limits and evaluate the square of the midpoint. If the square of the midpoint is greater than the number, the square root method must move the upper limit to the midpoint and similarly if the square of the midpoint is less than the number, it must move the lower limit to the midpoint.
After moving the appropriate limit, the square root method must evaluate a new midpoint and repeat the process until the desired precision is obtained.
The required precision for double precision floating point numbers is 8 significant digits. The precision at any iteration can be determined by dividing the difference between the limits by the lower limit.
When this is less than 1/108 any number between the limits will be an estimate of the square root of the number to the required precision. To minimize the error, the square root method should return the midpoint between the final limits that satisfy the precision requirement.
The square root method must return exact values for the special cases of zero and one.
If an application attempts to calculate the square root of a negative number, the square root method should display an appropriate message and terminate the program.
感谢任何帮助!
如果你用更多有效数字打印 upperLimit/lowerLimit
,你会看到它变得和 1.0000000000000002
一样小,但永远不会达到 1,这就是你的循环永远不会结束的原因。
而不是停留在循环中只要:
upperLimit/lowerLimit != 1
您应该将条件更改为:
while (upperLimit - lowerLimit > 0.0000000001)
这将在限制彼此足够接近时退出循环。
这就是第 6 步中“8 位有效数字”的含义 - 您的近似值应该使前 8 位有效数字正确。