C++使用Taylor系列扩展近似正弦的功能
嗨,我正在尝试计算正弦泰勒级数扩展的结果,从而在指定的术语中计算。
我遇到了一些问题,
- 您的任务是实现MakeIneToorder(k),
- 这是通过计算中使用的值类型来模板的。
- 它必须产生一个函数,该函数具有指定类型的值并
- 返回该值的正弦(在指定类型中)
double factorial(double long order){
#include <iostream>
#include <iomanip>
#include <cmath>
double fact = 1;
for(int i = 1; i <= num; i++){
fact *= i;
}
return fact;
}
void makeSineToOrder(long double order,long double precision = 15){
double value = 0;
for(int n = 0; n < precision; n++){
value += pow(-1.0, n) * pow(num, 2*n+1) / factorial(2*n + 1);
}
return value;
int main()
{
using namespace std;
long double pi = 3.14159265358979323846264338327950288419716939937510L;
for(int order = 1;order < 20; order++) {
auto sine = makeSineToOrder<long double>(order);
cout << "order(" << order << ") -> sine(pi) = " << setprecision(15) << sine(pi) << endl;
}
return 0;
}
我尝试调试
Hi I am trying to calculate the results of the Taylor series expansion for sine to the specified number of terms.
I am running into some problems
- Your task is to implement makeSineToOrder(k)
- This is templated by the type of values used in the calculation.
- It must yield a function that takes a value of the specified type and
- returns the sine of that value (in the specified type again)
double factorial(double long order){
#include <iostream>
#include <iomanip>
#include <cmath>
double fact = 1;
for(int i = 1; i <= num; i++){
fact *= i;
}
return fact;
}
void makeSineToOrder(long double order,long double precision = 15){
double value = 0;
for(int n = 0; n < precision; n++){
value += pow(-1.0, n) * pow(num, 2*n+1) / factorial(2*n + 1);
}
return value;
int main()
{
using namespace std;
long double pi = 3.14159265358979323846264338327950288419716939937510L;
for(int order = 1;order < 20; order++) {
auto sine = makeSineToOrder<long double>(order);
cout << "order(" << order << ") -> sine(pi) = " << setprecision(15) << sine(pi) << endl;
}
return 0;
}
I tried debugging
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个至少编译并给出一些输出的版本,
不确定那个奇怪的 sine(pi) 应该做什么
here is a version that at least compiles and gives some output
not sure what that odd
sine(pi)
was supposed to be doingApart the obvious syntax errors (the includes should be before your factorial header) in your code:
I see no templates in your code which your assignment clearly states to use
所以我希望模板像:
using
pow
for generic datatype is not safe因为Inbuild
POW
将使用float或double而不是您的通用类型,因此您可能会在不兼容类型的情况下期待舍入/铸造问题,甚至无法解决的功能。要补救您可以将代码重写以不使用
POW
作为其循环中的乘法,所以为什么一次又一次地计算POW?使用阶乘功能是废物
您可以在同一循环中类似于
pow
类似的计算,无需一次又一次地计算已经计算的乘法。同样,不为您的阶乘使用模板与使用pow
的问题相同
so putting all together using this formula:
along with templates and exchanging pow,factorial functions with consequent iteration I got this:
因此,阶乘
ii
乘以i+1
和i+2
每个迭代和电源xi
乘以< code>x^2 every iteration ... the sign change is hard coded so for loop does 2 iterations per one run (that is the reason for thebreak;
)As you can see this does not use anything funny so you do not need any includes for this not even math ...
You might want to add
x=fmod(x,6.283185307179586476925286766559)
at the start ofmysin< /code>为了使用不仅仅是第一阶段,但是在这种情况下,您必须确保
fmod
实现使用t
或与之兼容的类型...也是2* pi constant should be in target precision or higherbeware too big
n
will overflow bothint
and generic typeT
(so you might want to limit <代码> n 基于使用的类型,或者只是明智地使用它)。还要在32bit
float
上注意,无论使用这种计算,无论n
是什么。顺便提一句。有更快,更准确的方法来计算GonioMetrics,例如“ nofollow noreferrer”> chebyshev> chebyshev //www.google.com/url?sa = t&amp; rct=j&; q =&q =&amp; eSrc = s&amp; 3A%2F%2fen.wikipedia.org%2fwiki%2fcordic&amp; usg = aovvaw3xgz110jrbjjr1r5j7ftizw“ rel =“ nofollow noreferrer”> cordic
Apart the obvious syntax errors (the includes should be before your factorial header) in your code:
I see no templates in your code which your assignment clearly states to use
so I would expect template like:
using
pow
for generic datatype is not safebecause inbuild
pow
will use float or double instead of your generic type so you might expect rounding/casting problems or even unresolved function in case of incompatible type.To remedy that you can rewrite the code to not use
pow
as its just consequent multiplication in loop so why computing pow again and again?using factorial function is waste
you can compute it similar to
pow
in the same loop no need to compute the already computed multiplications again and again. Also not using template for your factorial makes the same problems as usingpow
so putting all together using this formula:
along with templates and exchanging pow,factorial functions with consequent iteration I got this:
so factorial
ii
is multiplied byi+1
andi+2
every iteration and powerxi
is multiplied byx^2
every iteration ... the sign change is hard coded so for loop does 2 iterations per one run (that is the reason for thebreak;
)As you can see this does not use anything funny so you do not need any includes for this not even math ...
You might want to add
x=fmod(x,6.283185307179586476925286766559)
at the start ofmysin
in order to use more than just first period however in that case you have to ensurefmod
implementation usesT
or compatible type to it ... Also the 2*pi constant should be in target precision or higherbeware too big
n
will overflow bothint
and generic typeT
(so you might want to limitn
based on used type somehow or just use it wisely).Also note on 32bit
float
s you can not get better than 5 decimal places no matter whatn
is with this kind of computation.Btw. there are faster and more accurate methods of computing goniometrics like Chebyshev and CORDIC