序列的总和
我正在尝试制定一个功能来计算此公式
#include <iostream>
#include <vector>
double Sequence(std::vector < double > & a) {
double result = 0;
for (int i = a.size() - 1; i > 0; i--) {
if (a[i] == 0) throw std::domain_error("Dividing with 0");
if (i > 1)
result += 1 / (a[i - 1] + 1 / a[i]);
else result += a[i - i];
std::cout << a[i] << " " << result << " " << "\n";
}
return result;
}
int main() {
std::vector<double>a{1,2,3,4,5};
try {
std::cout << Sequence(a);
} catch (std::domain_error e) {
std::cout << e.what();
}
return 0;
}
但是,如果我在该序列结果中添加了几个数字,则会出错。您能帮我解决这个问题吗?
I'm trying to make a function for calculating this formula
#include <iostream>
#include <vector>
double Sequence(std::vector < double > & a) {
double result = 0;
for (int i = a.size() - 1; i > 0; i--) {
if (a[i] == 0) throw std::domain_error("Dividing with 0");
if (i > 1)
result += 1 / (a[i - 1] + 1 / a[i]);
else result += a[i - i];
std::cout << a[i] << " " << result << " " << "\n";
}
return result;
}
int main() {
std::vector<double>a{1,2,3,4,5};
try {
std::cout << Sequence(a);
} catch (std::domain_error e) {
std::cout << e.what();
}
return 0;
}
Code gives correct result for {1,2,3} sequence. However, if I add a few numbers to that sequence result becomes wrong. Could you help me to fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是错误的;如果您有三个或更少的条款,它恰好可以工作。
您实际想要的复发是
您可以看到,这是正确的复发。
整理更多的东西(从环内部删除条件,在循环终止条件下逐个固定,并纠正异常条件):
This is wrong; it just happens to work if you have three or fewer terms.
The recurrence you actually want is
you can see that this is a correct recurrence by the fact that
Tidying up some more things (removing the condition from inside the loop, fixing an off-by-one in the loop termination condition, and correcting the exception condition):
这被称为简单的持续分数,所有分子都是1s。它基本上表示为
[a1; a2,a3,...,a]
(实际上是从a0
开始的,但无论如何)。您可以在每个步骤中从上到下计算结果,同时获得更好的近似值(或 convergent )。这个有限的理性系列或无限非理性系列的结果表示为
p/q
。为了计算您假设的结果,并且每个下一个
p_x/q_x
为结果提供了更好的appoximation。说如果给您一个小数,例如
1.425
在持续的分数胶囊中,则如A.KA
,那么如上所述计算的中间收敛;
请注意,每个收敛性的过冲都从最终结果中上下移动,随后除了
57/40
是1.425
的最小合理表达式。持续的分数是一个非常深的话题,实际上,一旦以持续的分数形式建立了算术,就消除了浮点误差。
您可以玩在这里。
This known as the Simple Continued Fraction where all numerators are 1s. It is basically represented as
[a1;a2,a3,...,an]
(well in fact starts froma0
but whatever). You can calculate the result from top to bottom while obtaining a better approximation (or convergent as they call it) at each step.The result of this finite rational series or infinite irrational series is expressed as
p/q
. In order to calculate the result you assumeand every next
p_x/q_x
yields a better appoximation to the result.Say if you are given a decimal like
1.425
in continued fraction cefficients asa.k.a.
Then the intermediate convergents calculated as described above would be;
Notice that each convergent overshoots up and down from the final result subsequently besides
57/40
being the minimal rational expression of1.425
.Continued fractions are a very deep topic which in fact eliminates the floating point error once an arithmetic is establised among them in their continued fractions form.
You can play here.