繁体   English   中英

编写手动平方根函数?

[英]Programming a manual square root function?

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

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

发生这种情况的原因是上限和下限(b和a)没有变化。 理想情况下,边界将是当前x和先前的x,从而创建新的x。 发生的事情是,当前电流x和a或b当前为常数,形成了新的x。

我已经尝试了很长时间,但是还没有找到一种“记住”或找到“上一个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 ;
}

我尚未找到一种方式来“记住”或找到“先前的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 ;
}

您还需要更新边界:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM