用牛顿法确定平方根

时间:2014-02-09 22:29:21

标签: java while-loop newtons-method

这是一项家庭作业,用于估算用户输入的数字的平方根,使用牛顿方法,该方法应返回< 0.0001。当我运行代码并输入一个数字时,之后没有任何反应。在调试模式下,'value'会增加,这与我想要的相反。提前谢谢。

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;  
    }
}

2 个答案:

答案 0 :(得分:0)

您的循环不会终止,因为您的条件是

while (Number >= 0.0)

如果您在满足条件时实际退出该功能,那就没关系了:

   if (value < .0001)
        // return sqrRoot as the answer
        return sqrRoot;

所以 - 改变最后一行,它会起作用。

演示: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
}

答案 1 :(得分:0)

工程师和学童以前使用的巨大的平方根表 到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;
    }
}