在C ++中使用Taylor's Series查找sinx值

问题描述 投票:1回答:2

我试图在C ++中编写一个代码块,用Taylor系列计算sinX值。

#include <iostream>
using namespace std;
// exp example
#include <cstdio>      // printf
#include <cmath>       //  exp

double toRadians(double angdeg)         //convert to radians to degree
{                                       //x is in radians
    const double PI = 3.14159265358979323846;
    return angdeg / 180.0 * PI;
}

double fact(double x)       //factorial function 
{                           //Simply calculates factorial for denominator
    if(x==0 || x==1)
        return 1;
    else
        x * fact(x - 1);
}

double mySin(double x)      //mySin function
{
    double sum = 0.0;
    for(int i = 0; i < 9; i++)
    {
        double top = pow(-1, i) * pow(x, 2 * i + 1);  //calculation for nominator
        double bottom = fact(2 * i + 1);              //calculation for denominator
        sum = sum + top / bottom;                     //1 - x^2/2! + x^4/4! - x^6/6!
    }
    return sum;
}

int main()
{
    double param = 45, result;

    result = mySin(toRadians(param)); //This is my sin value
    cout << "Here is my homemade sin : " << result << endl;

    result = sin(param);              //This is library value
    cout << "Here is the API sin : " << result << endl;

    return 0;
}

所以我的程序没有任何错误。我的输出完全是:

这是我自制的罪恶:南 这是API罪:0.850904

我知道我犯了一个很大的逻辑错误,但我找不到它。这是我使用C ++的第二周。我对Java更熟悉。我编写了同样的东西,它的工作绝对完美。答案相互匹配。感谢您的时间和关注!

c++ sin taylor-series
2个回答
3
投票
  1. fact,你错过了回报:x*fact(x-1);应该是return x*fact(x-1);。如果打开警告,您可以看到编译器抱怨。例如,对于GCC,调用g++ -Wall program.cpp给出了因子函数的Warning: control reaches end of non-void function
  2. API sin也需要以弧度表示的角度,因此将result=sin(param);更改为result=sin(toRadians(param));。通常,如果对API有疑问,请查阅文档,如here

0
投票

你的代码似乎有一些逻辑错误。这是我纠正的一个:

#include <iostream>

using namespace std;

double radians(double degrees)  // converts degrees to radians
{
    double radians;
    double const pi = 3.14159265358979323846;
    radians = (pi/180)*degrees;
    return radians;
}

double factorial(int x)  //calculates the factorial
{
    double fact = 1;
    for(; x >= 1 ; x--)
    {
        fact = x * fact;
    }
    return fact;
}

double power(double x,double n)  //calculates the power of x
{
    double output = 1;
    while(n>0)
    {
         output =( x*output);
         n--;
    }
    return output;
}

float sin(double radians)  //value of sine by Taylors series
{
   double a,b,c;
   float result = 0;
   for(int y=0 ; y!=9 ; y++)
   {
      a=  power(-1,y);
      b=  power(radians,(2*y)+1);
      c=  factorial((2*y)+1);
      result = result+ (a*b)/c;
   }
   return result;
}

double n,output;

int main()
{
    cout<<"enter the value\t";
    cin>>n;

    n = radians(n);
    cout<< "\nthe value in radians is\t"<< n << "\n";

    output = sin(n);

    cout<< "\nsine of the given value is\t"<< output;
    return 0;
}

这个程序的目的是使用自定义函数而不是库来简化其他人的学习。

在这个程序中有四个用户定义的函数。前三个用户定义的函数'radians()','factorial()','power()',显然是简单的函数,按照其名称建议执行操作。

第四个函数'sin()'以函数'radians()'给出的弧度输入。 sin函数在函数'for(int y = 0; y!= 9; y ++)'循环中使用Taylors系列迭代项,直到九次迭代来计算输出.'for()'循环迭代一般数学表达式:项(n)=(( - 1)^ N)(的x ^(2N + 1))/(2N + 1)!

© www.soinside.com 2019 - 2024. All rights reserved.