简体   繁体   English

通过c ++中的迭代查找平方根

[英]Finding a square root through iteration in c++

I have a homework assignment due and I am having trouble with my loops. 我有一个家庭作业到期,我的循环遇到了麻烦。 I first have to find the next highest and lowest square roots through loops which I already can do. 我首先必须通过循环找到下一个最高和最低的平方根,我已经可以做了。 Next my assignment tells me that I need to get an approximation of the square root which I do by averaging the next highest and lowest square roots of the integer. 接下来,我的任务告诉我,我需要通过平均整数的下一个最高和最低平方根得到平方根的近似值。 Then I must ask the user for the number of decimal places of accuracy they want. 然后我必须询问用户他们想要的精确小数位数。 Here is a quote from the assignment: 以下是作业的引用:

A count-controlled loop should then be constructed; 然后应构建一个计数控制的循环; it will execute once for each of the desired decimal positions; 它会对每个所需的小数位执行一次; in the example, this loop will execute four times (once each for the tenths, hundredths, thousandths, and ten-thousandths decimal positions). 在这个例子中,这个循环将执行四次(每次十分之一,千分之一,千分之一和十分之一小数位)。 Use a counter such as decimalPosition to keep track of which pass the loop is on. 使用decimalPosition等计数器来跟踪循环所在的传递。

This is where I have trouble, I am using a while loop based on the number of decimal positions the user has entered but my loop doesn't complete the loop. 这是我遇到麻烦的地方,我使用的是基于用户输入的小数位数的while循环,但是我的循环没有完成循环。 I am new to programming so please forgive me if this is a truly simple. 我是编程新手所以如果这真的很简单,请原谅我。 Here is my while code: 这是我的代码:

for (int decimalPosition = 1; decimalPosition <= decimal; decimalPosition++)
{
    while (baseRoot*baseRoot > num)
    {
        baseRoot = baseRoot - (pow((.1),decimalPosition));
        cout << fixed << setprecision(decimal) << baseRoot << endl;
    }

}

here is my output so far 到目前为止这是我的输出

Enter a number you wish to know the square root of: 8
Enter the number of decimal places of accuracy you want: 7
Find the square root of 8 to 7 decimal places: 
2.6000000
2.7000000
2.8000000
2.9000000
2.9000000 square root of 8.0000000

It's called Newton's method , and its convergence is quadratic. 它被称为牛顿方法 ,其收敛性是二次方的。 That should help you figure it out. 这应该可以帮助你解决这个问题。

PS - The Babylonians discovered it first, but Newton gets credit for it. PS--巴比伦人首先发现了它,但牛顿因此得到了认可。

To make your loop works, you can add a statement 要使循环有效,可以添加语句

baseRoot = baseRoot + (pow((.1),decimalPosition));

after the while loop because your need to make sure baseRoot is bigger than the answer before each iteration. 在while循环之后因为你需要确保在每次迭代之前baseRoot大于答案。 Like this: 像这样:

for (int decimalPosition = 1; decimalPosition <= decimal; decimalPosition++)
{
    while (baseRoot*baseRoot > num)
    {
        baseRoot = baseRoot - (pow((.1),decimalPosition));
        cout << fixed << setprecision(decimal) << baseRoot << endl;
    }
    baseRoot = baseRoot + (pow((.1),decimalPosition));
}

Now you can get the answer 2.8284271 . 现在你可以得到答案2.8284271

By the way, there's another efficient way called bisection method (similar to binary search) to solve this kind of problem (related to Monotonic function ) without too much math: 顺便说一下,还有另一种称为二分法的有效方法 (类似于二分搜索)来解决这类问题(与单调函数有关 )而没有太多的数学:

double mySqrt(double x, double epsilon) {
    double left = 0, right = x;
    while (right - left > epsilon) {
        double mid = (left + right) / 2;
        if (mid * mid > x) {
            right = mid;
        } else {
            left = mid;
        }
    }
    return left;
}

It's simple, stupid :) 这很简单,很愚蠢:)

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

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