Java 使用牛顿法确定平方根
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21665940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Determining Square Root Using Newton's Method
提问by Grafica
This is a homework assignment, to estimate the square root of a number input by the user, using Newton's method, which should return a result of < .0001. When I run the code and enter a number, nothing happens after that. In debug mode, the 'value' increases, which is the opposite of what I want it to do. Thanks in advance.
这是一个家庭作业,使用牛顿方法估计用户输入的数字的平方根,应该返回 < .0001 的结果。当我运行代码并输入一个数字时,之后没有任何反应。在调试模式下,“值”会增加,这与我想要它做的相反。提前致谢。
import java.text.DecimalFormat;
import java.util.Scanner;
public class Newton {
public static void main(String[] args)
{
// declare a Scanner class object
Scanner sc = new Scanner(System.in);
// declare a DecimalFormat class object
DecimalFormat fourDecimal = new DecimalFormat("0.0000");
float Number = 0;
System.out.println("Program: find square roots by Newton's Method");
System.out.println("Please enter a number: ");
Number = sc.nextFloat();
System.out.println("The square root of " + Number + " is " + fourDecimal.format(Compute(Number)));
}
public static float Compute(float Number)
{
// define variable sqrRoot to hold the approximate square root
float sqrRoot = 0;
// define temporary variable temp to hold prior value of iteration
float temp = 0;
// divide variable num by 2 to start the iterative process
// and assign the quotient to temp
temp = Number/2;
// open a while() loop that continues as long as num >= 0.0
while (Number >= 0.0)
{
// construct the main iterative statement
sqrRoot = temp - (temp * temp - Number) / (2 * temp);
// open an if block to check if the absolute value of the difference of
// variables temp and sqrRoot is below a small sentinel value such as 0.0001
// if this condition is true then break the loop
float value;
value = Math.abs(temp - sqrRoot);
if (value < .0001)
// return sqrRoot as the answer
Number = sqrRoot;
// if this condition is not true then assign sqrRoot to temp
else temp = sqrRoot;
// close the while() loop
}
return Number;
}
}
采纳答案by Floris
Your loop will not terminate because your condition is
您的循环不会终止,因为您的条件是
while (Number >= 0.0)
That would be OK if you actually exited the function when your condition is met:
如果您在满足条件时实际退出该函数,那将是可以的:
if (value < .0001)
// return sqrRoot as the answer
return sqrRoot;
So - change that last line and it will work.
所以 - 更改最后一行,它将起作用。
Demo : http://ideone.com/XzJXLv
public static float Compute(float Number)
{
// define variable sqrRoot to hold the approximate square root
float sqrRoot = 0;
// define temporary variable temp to hold prior value of iteration
float temp = 0;
// divide variable num by 2 to start the iterative process
// and assign the quotient to temp
temp = Number/2;
// open a while() loop that continues as long as num >= 0.0
while (Number >= 0.0) // <<<< you might reconsider this condition: iteration count?
{
// construct the main iterative statement
sqrRoot = temp - (temp * temp - Number) / (2 * temp);
// open an if block to check if the absolute value of the difference of
// variables temp and sqrRoot is below a small sentinel value such as 0.0001
// if this condition is true then break the loop
float value;
value = Math.abs(temp - sqrRoot);
if (value < .0001)
// return sqrRoot as the answer
return sqrRoot; // <<<<< this is the line you needed to change
// if this condition is not true then assign sqrRoot to temp
else temp = sqrRoot;
} // close the while() loop
return Number; // <<<<< you will never reach this line
}
回答by Miller Cy Chan
The huge tables of square roots used by engineers and schoolchildren prior to the 1970s have now been replaced by a five-line program for Newton's method.
工程师和学童在 1970 年代之前使用的巨大平方根表现在已被牛顿方法的五行程序所取代。
public static Number squareRoot(Number value) {
double temp = value.doubleValue() / 2;
for(double sqrRoot; ; temp = sqrRoot) {
sqrRoot = temp - (temp * temp - value.doubleValue()) / (2 * temp);
if (Math.abs(temp - sqrRoot) < 1e-10) return sqrRoot;
}
}