سؤال

I am trying to write a block of codes in C++ that calculates sinX value with Taylor's series.

#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;
}

So my program works without any error. My output is exactly:

Here is my homemade sin : nan
Here is the API sin:0.850904

I know I am making a big logic mistake but I couldn't find it out. It is my second week with C++. I am more familiar with Java. I coded the same thing and It worked absolutely perfect. The answers matched each other. Thanks for your time and attention!

هل كانت مفيدة؟

المحلول

  1. in fact, you miss the return: x*fact(x-1); should be return x*fact(x-1);. You can see the compiler complaining if you turn the warnings on. For example, with GCC, calling g++ -Wall program.cpp gives Warning: control reaches end of non-void function for the factorial function.

  2. The API sin also needs the angle in radians, so change result=sin(param); into result=sin(toRadians(param));. Generally, if in doubt about the API, consult the docs, like here.

نصائح أخرى

Your codes seems to have some logical mistakes. Here is my corrected one:

#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;
}

The intention of this program was to use custom functions instead of libraries to make learning for others easy.

There are four user defined functions in this program.The first three user defined functions 'radians()', 'factorial()','power()', are apparently simple functions that perform operations as their name suggests.

The fourth function 'sin()' takes input in radians given by the function 'radians()'. The sin function uses Taylors series iterated term wise in the function's 'for(int y= 0;y!=9;y++)' loop till nine iterations to calculate the output.The 'for()' loop iterates the general mathematical expression: Term(n)=((-1)^n).(x^(2n+1))/(2n+1)!

sin(x)= x- x^3/3! + x^5/5! -x^7/7! + x^9/9!
=x-x^3/2*3 (1- x^2/4*5 + x^4/4*5*6*7 + x^6/4*5*6*7*8*9)
=x - x^3/2*3 {1- x^2/4*5(1- x^2/6*7 + x^4/6*7*8*9)}
=x - x^3/2*3 [{1- x^2/4*5 ( 1- x^2/6*7 (1- x^2/8*9))}]
=x(1 - x^2/2*3 [{1- x^2/4*5 ( 1- x^2/6*7 (1- x^2/8*9))}])

double sin_series_recursion(double x, int n){
    static double r=1;

    if(n>1){

        r=1-((x*x*r)/(n*(n-1)));

        return sin_series_recursion(x,n-2);


    }else return r*x;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top