C++ 计算C+中3的平方根+;
以下是我尝试过的:C++ 计算C+中3的平方根+;,c++,C++,以下是我尝试过的: #include <iostream> #include <cmath> int main(void) { double prevRes(1); double res(1 + 1./2); short i(2); while (abs(prevRes - res) > 1.e-14) { prevRes = res; res = i + 1 / res;
#include <iostream>
#include <cmath>
int main(void)
{
double prevRes(1);
double res(1 + 1./2);
short i(2);
while (abs(prevRes - res) > 1.e-14)
{
prevRes = res;
res = i + 1 / res;
i = 3 - i;
}
std::cout << res << std::endl;
return 0;
}
#包括
#包括
内部主(空)
{
双前置(1);
双精度(1+1./2);
短i(2);
而(abs(prevRes-res)>1.e-14)
{
prevRes=res;
res=i+1/res;
i=3-i;
}
std::cout三分之二的sqrt(bool加上两个,int rec_depth,int max rec_depth)
{
int x;
如果(记录深度<2)
x=1;
其他的
x=加2?2:1;
if(记录深度<最大记录深度)
返回x+1/sqrt_,共三个(!加上两个,+rec_深度,max_rec_深度);
返回x;
}
这个方法可以通过估计一个阈值来调用
int main()
{
std::cout << sqrt_of_three(true, 0, 10);
}
intmain()
{
std::cout如果我将其与比较,公式似乎有点错误。注意,1+在开始时重复
接下来,我们可以使用递归函数来执行计算并提供大量迭代。请注意,我们可以使用较大的返回值来终止递归(甚至为零,但这需要更多的迭代,因为从技术上讲,这是错误的假设)
最后,我们继续尝试更多的迭代,直到误差足够小
#include <iostream>
#include <limits>
#include <cmath>
double f(int depth, bool odd = true)
{
if(depth == 0)
return std::numeric_limits<double>::max();
return (odd ? 1 : 2) + 1. / f(--depth, !odd);
}
double sqrt3(int depth = 10)
{
return 1 + 1. / f(depth);
}
int main(void)
{
int depth{2};
double prevRes{sqrt3(depth)};
double res{sqrt3(++depth)};
while(abs(prevRes - res) > 1.e-14)
{
prevRes = res;
res = sqrt3(++depth);
}
std::cout << "Answer is " << res << " at depth " << depth << ".\n";
}
使用内置的sqrt函数
#include <cstdio>
#include <cmath>
int main ()
{
double param, result;
param = 1024.0;
result = sqrt (param);
printf ("sqrt(%f) = %f\n", param, result );
return 0;
}
#包括
#包括
int main()
{
双参数,结果;
参数=1024.0;
结果=sqrt(参数);
printf(“sqrt(%f)=%f\n”,参数,结果);
返回0;
}
遗憾的是,这个问题没有经过更多的努力和细节才被认真对待。我一直对连分数的出现和概念感到困惑,但花时间思考并实现连分数是件好事
这个特殊的一个可以迭代完成。正如@wally所说,问题中显示的连分式不收敛于sqrt(3),而是收敛于~1.36603。最上面的两个系数应该是1。请注意sqrt(3)~=1+(1/1.36603),连分式中的所有系数都是交替的
因此,如果一个循环自下而上工作,直到交替连分式收敛,那么在循环结束后再进行一次计算,我们将得到正确的答案。在每次迭代中,当前值的倒数加上1或2。初始值可以是任何值
#include <iostream>
#include <cmath>
#include <limits>
// Calculate square root of 3 with a continued fraction
int main(void) {
int iterations = 0;
double epsilon = 1.0e-12; //error bounds
double prev = 0.0;
double curr = 1.0; //initial estimate
double error = curr - prev;
// Don't show more precision than we have
std::cout.precision(std::numeric_limits<double>::digits10);
// Iterate the continued fraction [1;1,2,1,2...]
// from the bottom up until it converges with errors
// less than epsilon.
while (std::abs(error) > epsilon) {
prev = curr;
// Unroll the loop with the repeating pattern here
curr = 2 + (1/curr);
curr = 1 + (1/curr);
iterations++;
error = curr - prev;
std::cout << "error at iteration " << iterations
<< " was " << error << std::endl;
}
// The actual continued fraction we want to evaluate
// is [1;1,1,2,1,2,...].
// The two top-level coefficients are 1, so do
// another half iteration here.
curr = 1 + (1/curr);
std::cout << "sqrt(3) = " << curr << " after "
<< iterations << " iterations" << std::endl;
return 0;
}
#包括
#包括
#包括
//用连分数计算3的平方根
内部主(空){
int迭代次数=0;
双ε=1.0e-12;//错误界限
双上指数=0.0;
双电流=1.0;//初始估算
双错误=当前-上一次;
//不要比我们表现得更精确
标准::计算精度(标准::数值限制::数字10);
//迭代连分数[1;1,2,1,2…]
//自下而上,直到收敛到错误
//小于ε。
而(标准::abs(错误)>ε){
上一次=当前;
//在此处使用重复模式展开循环
电流=2+(1/电流);
电流=1+(1/电流);
迭代++;
错误=当前-上一个;
这里有什么问题吗?你不是刚刚发布了同样的问题吗(后来被删除了)?请不要这样做。您应该编辑原始问题以改进它。现在您必须编辑此问题,以解释您的尝试存在的问题。是否出现生成错误?意外结果?其他问题?预期和实际输出是什么?您尝试过吗?程序不会永远运行。它会终止immE的输出为1.5
。解决此关系需要递归方法,因为第一级除法下的术语取决于第二级的答案,而第二级的答案又取决于第三级的答案,依此类推……注释往往会解释其可能被否决的原因。请注意那些发表评论的人不一定是投反对票的人。问题一开始是问如何做一些一般性的事情(我如何计算它?),而不是问一些具体的事情(为什么程序永远运行?)。此外,可以通过简单调试解决的问题往往会被否决。也就是说,用清晰的问题陈述寻求调试帮助仍然是讨论的主题,但任何问题都可能因为缺乏(感知的)支持而被否决努力。请不要亲自投票。你似乎可以在基本情况下返回任何值。数学……问题是用给定的公式来解决问题,而不是用Buffin函数。@ Nova,它在哪里?我所看到的是,“如何用下面的关系计算C++中的3的平方根?”,这是内置函数实现的。对于谷歌搜索“sqrt C++”的人来说,我们绝对不希望有人想出一个重新发明轮子的答案。如果这是一个学术练习,有学术要求,OP需要明确说明这些要求。例如,“这是一个学术执行”和“我不允许在本次学术练习中使用内置函数如果buildin函数足够的话,提到以下关系是没有用的。当你不关心根是如何计算的时候,为什么要这样说呢?是的,我把这个问题理解为学术练习或家庭作业。是的,你的答案对谷歌搜索“sqrt C++”的人来说是最好的,但我相信在谷歌搜索之后,没有人会看到这个问题。他显然是一个刚刚起步的人,所以他可能甚至不知道这个问题的存在。我的答案反映了问题的原貌。我的所有答案也是如此。我不读字里行间的东西。我是一名程序员。我处理逻辑问题。目前,这是以下搜索中的第四个热门话题:你知道吗nt要争论吗?我需要进一步的争论。像这样的事情不要使用递归。请。@RustyX你会如何使用连续分数关系呢?@Philipp查看我的迭代实现答案。
#include <cstdio>
#include <cmath>
int main ()
{
double param, result;
param = 1024.0;
result = sqrt (param);
printf ("sqrt(%f) = %f\n", param, result );
return 0;
}
#include <iostream>
#include <cmath>
#include <limits>
// Calculate square root of 3 with a continued fraction
int main(void) {
int iterations = 0;
double epsilon = 1.0e-12; //error bounds
double prev = 0.0;
double curr = 1.0; //initial estimate
double error = curr - prev;
// Don't show more precision than we have
std::cout.precision(std::numeric_limits<double>::digits10);
// Iterate the continued fraction [1;1,2,1,2...]
// from the bottom up until it converges with errors
// less than epsilon.
while (std::abs(error) > epsilon) {
prev = curr;
// Unroll the loop with the repeating pattern here
curr = 2 + (1/curr);
curr = 1 + (1/curr);
iterations++;
error = curr - prev;
std::cout << "error at iteration " << iterations
<< " was " << error << std::endl;
}
// The actual continued fraction we want to evaluate
// is [1;1,1,2,1,2,...].
// The two top-level coefficients are 1, so do
// another half iteration here.
curr = 1 + (1/curr);
std::cout << "sqrt(3) = " << curr << " after "
<< iterations << " iterations" << std::endl;
return 0;
}