C++使用Taylor系列扩展近似正弦的功能

发布于 2025-01-19 03:42:54 字数 1090 浏览 5 评论 0原文

嗨,我正在尝试计算正弦泰勒级数扩展的结果,从而在指定的术语中计算。

我遇到了一些问题,

  • 您的任务是实现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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苏大泽ㄣ 2025-01-26 03:42:54

这是一个至少编译并给出一些输出的版本,

#include <iostream> 
#include <iomanip> 
#include <cmath> 
using namespace std;
double factorial(double long num) {
    double fact = 1;
    for (int i = 1; i <= num; i++) {
        fact *= i;
    }
    return fact;
}

double makeSineToOrder(double num, 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(){

    long double pi = 3.14159265358979323846264338327950288419716939937510L;
    for (int order = 1; order < 20; order++) {
        auto sine = makeSineToOrder(order);
        cout << "order(" << order << ") -> sine(pi) = " << setprecision(15) << sine << endl;
    }
    return 0;
}

不确定那个奇怪的 sine(pi) 应该做什么

here is a version that at least compiles and gives some output

#include <iostream> 
#include <iomanip> 
#include <cmath> 
using namespace std;
double factorial(double long num) {
    double fact = 1;
    for (int i = 1; i <= num; i++) {
        fact *= i;
    }
    return fact;
}

double makeSineToOrder(double num, 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(){

    long double pi = 3.14159265358979323846264338327950288419716939937510L;
    for (int order = 1; order < 20; order++) {
        auto sine = makeSineToOrder(order);
        cout << "order(" << order << ") -> sine(pi) = " << setprecision(15) << sine << endl;
    }
    return 0;
}

not sure what that odd sine(pi) was supposed to be doing

无敌元气妹 2025-01-26 03:42:54

Apart the obvious syntax errors (the includes should be before your factorial header) in your code:

  1. I see no templates in your code which your assignment clearly states to use

    所以我希望模板像:

     &lt;类T&gt; t mysin(t x,int n = 15){...}
     
  2. using pow for generic datatype is not safe

    因为Inbuild POW将使用float或double而不是您的通用类型,因此您可能会在不兼容类型的情况下期待舍入/铸造问题,甚至无法解决的功能。

    要补救您可以将代码重写以不使用POW作为其循环中的乘法,所以为什么一次又一次地计算POW?

  3. 使用阶乘功能是废物

    您可以在同一循环中类似于pow类似的计算,无需一次又一次地计算已经计算的乘法。同样,不为您的阶乘使用模板与使用pow

    的问题相同

so putting all together using this formula:

taylor

along with templates and exchanging pow,factorial functions with consequent iteration I got this:

template <class T> T mysin(T x,int n=15)
    {
    int i;
    T y=0;      // result
    T x2=x*x;   // x^2
    T xi=x;     // x^i
    T ii=1;     // i!
    if (n>0) for(i=1;;)
        {
        y+=xi/ii; xi*=x2; i++; ii*=i; i++; ii*=i; n--; if (!n) break;
        y-=xi/ii; xi*=x2; i++; ii*=i; i++; ii*=i; n--; if (!n) break;
        }
    return y;
    }

因此,阶乘ii乘以i+1i+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 the break;)

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 of mysin< /code>为了使用不仅仅是第一阶段,但是在这种情况下,您必须确保fmod实现使用t或与之兼容的类型...也是2* pi constant should be in target precision or higher

beware too big n will overflow both int and generic type T (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:

  1. I see no templates in your code which your assignment clearly states to use

    so I would expect template like:

    <class T> T mysin(T x,int n=15){ ... }
    
  2. using pow for generic datatype is not safe

    because 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?

  3. 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 using pow

so putting all together using this formula:

taylor

along with templates and exchanging pow,factorial functions with consequent iteration I got this:

template <class T> T mysin(T x,int n=15)
    {
    int i;
    T y=0;      // result
    T x2=x*x;   // x^2
    T xi=x;     // x^i
    T ii=1;     // i!
    if (n>0) for(i=1;;)
        {
        y+=xi/ii; xi*=x2; i++; ii*=i; i++; ii*=i; n--; if (!n) break;
        y-=xi/ii; xi*=x2; i++; ii*=i; i++; ii*=i; n--; if (!n) break;
        }
    return y;
    }

so factorial ii is multiplied by i+1 and i+2 every iteration and power xi is multiplied by x^2 every iteration ... the sign change is hard coded so for loop does 2 iterations per one run (that is the reason for the break;)

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 of mysin in order to use more than just first period however in that case you have to ensure fmod implementation uses T or compatible type to it ... Also the 2*pi constant should be in target precision or higher

beware too big n will overflow both int and generic type T (so you might want to limit n based on used type somehow or just use it wisely).

Also note on 32bit floats you can not get better than 5 decimal places no matter what n is with this kind of computation.

Btw. there are faster and more accurate methods of computing goniometrics like Chebyshev and CORDIC

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文