【问题标题】:Programming a manual square root function?编程手动平方根函数?
【发布时间】:2017-09-21 10:49:29
【问题描述】:

对于我的班级,我正在使用包含来编写平方根函数。不,我不能使用任何其他方法...

到目前为止,这是我的代码,程序几乎可以运行。它适用于完美的平方根和其他一些值(如 11 或 5),但对于其他值(8、2)它会进入无限循环。

发生这种情况的原因是上限和下限(b 和 a)没有改变。理想情况下,边界将是当前 x 和先前的 x,创建新的 x。发生的情况是,新的 x 当前是由当前 x 和 a 或 b 形成的,是一个常数。

我已经尝试了很长时间,但我还没有找到“记住”或找到“以前的 x”的方法,因为每次 while 循环重复时,只有当前的 x 可以使用。有谁知道如何解决这样的问题?

void inclusion ()
{
    double v ;
    cout << "*** Now solving using Inclusion ***" << endl << "To calculate the square root, enter a positive number: " ;
    cin >> v ;

    while (v<0)
    {
        cout << "Square roots of negative numbers cannot be calculated, please enter a positive number: " ;
        cin >> v ;
    }

    cout << endl ;

    int n = 0;
    while (v >= n*n)
        n++ ;

    double b = n ;
    double a = n-1 ;

    int t = 0 ;
    double x = (a+b)/2 ;

        while ((x * x - v >= 0.1) || (x * x - v <= -0.1))
        {
            t++ ;

            if (x * x < v)
                {
                cout << "Lower Bound: " << x << '\t' << '\t' ;
                cout << "Upper Bound: " << b << '\t' << '\t' ;
                x = (b + x)/2 ;
                cout << "Approximation " << t << ": " << x  << endl ;
                }

            else
                {
                cout << "Lower Bound: " << a << '\t' << '\t' ;
                cout << "Upper Bound: " << x << '\t' << '\t' ;
                x = (a + x)/2 ;
                cout << "Approximation " << t << ": " << x  << endl ;
                }
        }

    cout << endl << "The answer is " << x << ". Iterated " << t << " times." << endl << endl ;
}

【问题讨论】:

  • 没有什么能阻止你拥有一个变量 previous_x,在计算新的 x 之前保存值。
  • 您需要在每次迭代时计算新的边界 a 和 b,而不是(仅)新的 x。

标签: c++ math inclusion


【解决方案1】:

我还没有找到“记住”或找到“以前的 x”的方法

在循环结束时有一个变量previous_x,你previous_x = x

但这不是你的问题。你正在改变x,而不是ab,所以你进入了一个无限重复的模式。相反,您应该调整使您更紧的界限。

void inclusion ()
{
    double v ;
    cout << "*** Now solving using Inclusion ***" << endl << "To calculate the square root, enter a positive number: " ;
    cin >> v ;

    while (v<0)
    {
        cout << "Square roots of negative numbers cannot be calculated, please enter a positive number: " ;
        cin >> v ;
    }

    cout << endl ;

    int n = 0;
    while (v >= n*n)
        n++ ;

    double b = n ;
    double a = n-1 ;

    int t = 0 ;

    double x;
    for (x = (a+b)/2; abs(x * x - v) >= 0.1; x = (a+b)/2, ++t)
    {
        if (x * x < v)
        {
            cout << "Lower Bound: " << x << '\t' << '\t' ;
            cout << "Upper Bound: " << b << '\t' << '\t' ;
            a = (b + x)/2 ;
            cout << "Approximation " << t << ": " << x  << endl ;
        }   
        else
        {
            cout << "Lower Bound: " << a << '\t' << '\t' ;
            cout << "Upper Bound: " << x << '\t' << '\t' ;
            b = (a + x)/2 ;
            cout << "Approximation " << t << ": " << x  << endl ;
        }
    }

    cout << endl << "The answer is " << x << ". Iterated " << t << " times." << endl << endl ;
}

【讨论】:

  • 我试过了,也许我没有像你说的那样正确地把它放进去,但它并没有让它工作......
【解决方案2】:

你也需要更新边界:

a = x;
x = (b + x)/2;

b = x;
x = (a + x)/2;

【讨论】:

  • 我明白发生了什么。当我还没有将 a 和 b 更改为 double 时,我完全尝试过。由于它们是整数,它不起作用,但现在它起作用了。谢谢!你救了一条命!我必须在 5 分钟内提交:p
猜你喜欢
  • 2017-04-04
  • 1970-01-01
  • 1970-01-01
  • 2015-03-04
  • 2018-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多