C++ 数字的平方根。。。精确到n精度
我无法理解上述方法。有人能解释一下吗?我已经做了一些代码,但它仅限于一些硬编码精度,似乎消耗了太多的计算机资源C++ 数字的平方根。。。精确到n精度,c++,algorithm,numerical-methods,C++,Algorithm,Numerical Methods,我无法理解上述方法。有人能解释一下吗?我已经做了一些代码,但它仅限于一些硬编码精度,似乎消耗了太多的计算机资源 R = 0.00001 INPUT N WHILE R*R != N R = R + 0.00001 ENDWHILE PRINT R < > n个数的平方根的算法或C++代码是什么? 如果需要,可以从用户处获取n。有一些算法更适合计算机评估。我在20世纪60年代学会了这个问题,这是一种使用类似长除法的过程逐位手动计算平方根的方法 在计算结果的第n位数字期间,目标是找
R = 0.00001
INPUT N
WHILE R*R != N
R = R + 0.00001
ENDWHILE
PRINT R
< > n个数的平方根的算法或C++代码是什么?
如果需要,可以从用户处获取n。有一些算法更适合计算机评估。我在20世纪60年代学会了这个问题,这是一种使用类似长除法的过程逐位手动计算平方根的方法 在计算结果的第n位数字期间,目标是找到最大的前缀字符串,以便平方小于或等于输入的前2n位数字 关键的基本思想是
(a+b)^2=a^2+b^2+2ab
。在算法中,a
是到目前为止的部分结果,b
是新的数字。它通过将输入中的两个位置移动到结果中生成的一个数字,从而计算出平方100和根10的因子
在追加数字
d
之前,将p
作为部分结果。我们已经从输入中减去了p^2
。我们还需要减去d^2+2pd
,以保持新部分结果平方的减法。等效地,减去d(2p+d)
。我们保持p
已经加倍,追加d
,然后乘以d
。在进入下一步之前,我们需要同时使用代码< d>代码>。 这里是一段C++代码,虽然不是任意的精度,但它对你可能有用。它比您的基本代码更接近于一个完整的解决方案:
#include <iostream>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <climits>
const unsigned g_unPlaces = 8;
int main(int argc, char** argv)
{
if (argc != 2)
{
std::cerr << "USAGE: " << *argv << " NUMBER" << std::endl;
return 1;
}
std::vector<unsigned> vecInteger;
std::vector<unsigned> vecDecimal;
char *pDecimal = strchr(argv[1], '.');
// Read integer part of NUMBER
if (pDecimal == NULL) pDecimal = argv[1] + strlen(argv[1]);
if ((pDecimal - argv[1]) % 2) vecInteger.push_back(0);
for (char *pCurrent = argv[1]; pCurrent < pDecimal; ++pCurrent)
{
int nValue = *pCurrent - '0';
if (nValue >= 10 || nValue < 0)
{
std::cerr << "Error: Invalid character in input!" << std::endl;
return 1;
}
vecInteger.push_back((unsigned) nValue);
}
// Read decimal part of NUMBER
if (*pDecimal != '\0')
{
for (++pDecimal; *pDecimal != '\0'; ++pDecimal)
{
if (*pDecimal == '.')
{
std::cerr << "Error: Multiple decimals in input!" << std::endl;
return 1;
}
int nValue = *pDecimal - '0';
if (nValue >= 10 || nValue < 0)
{
std::cerr << "Error: Invalid character in input!" << std::endl;
return 1;
}
vecDecimal.push_back((unsigned) nValue);
}
if (vecDecimal.size() % 2) vecDecimal.push_back(0);
}
const unsigned unInteger = vecInteger.size();
const unsigned unDecimal = vecDecimal.size();
std::vector<unsigned> vecValues;
unsigned x, y = 0, c = 0, p = 0;
for (unsigned i = 0; i < g_unPlaces; ++i)
{
if (2*i < unInteger-1)
{
c = (c*100 - y*100) + vecInteger[i*2]*10 + vecInteger[i*2+1];
}
else if (2*i < unInteger+unDecimal-1)
{
c = (c*100 - y*100) + vecDecimal[i*2-unInteger]*10
+ vecDecimal[i*2+1-unInteger];
}
else
{
c = c*100 - y*100;
}
if (c == 0) break;
y = 0;
for (x = 1; x < 10; ++x)
{
unsigned temp = x*(20*p + x);
if (temp > c) { --x; break; }
y = temp;
}
p = 10*p + x;
vecValues.push_back(x);
}
// Write the result
for (unsigned i = 0; i < unInteger/2; ++i)
{
std::cout << vecValues[i];
}
std::cout << '.';
for (unsigned i = unInteger/2; i < vecValues.size(); ++i)
{
std::cout << vecValues[i];
}
std::cout << std::endl;
return 0;
}
#包括
#包括
#包括
#包括
#包括
常量无符号g_unPlaces=8;
int main(int argc,字符**argv)
{
如果(argc!=2)
{
std::cerr使用您能使用的最大精度类型(可能通过使用或类似的库),然后以所需的精度打印出来。无需重新发明轮子。到目前为止,您的代码是BASIC的,并且没有真正遵循图片中描述的任何算法。您在理解具体部分时有困难吗(如果是数学问题,你也可以在math.stackexchange.com上提问)。如果这不是家庭作业,只需使用合适的库提供的sqrt
函数,而不是编写自己的函数。另一种求平方根的方法:该算法可能永远不会工作。至少,它应该可以执行R*R@matstpeterson该算法是为手动计算而设计的,使用精确的十进制算法,因此不涉及微积分。如果正确遵循这些步骤,部分结果将始终小于或等于输入的平方根。是的,二项式定理也是一种实现方法,但您能解释更多关于长除法过程的信息吗?提前感谢您的帮助。