Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sine Function without any library

Tags:

c++

c++11

I am new at learning c++. Because of curiosity, I created a sine function without using any library (except iostream, to take input and output). Here is the code that I wrote, and it works perfectly.

    #include<iostream>
using namespace std;
int factorial(int n);
double sin(double x, int n);
double pow(double x, int n);
int main(){
    double num;
    int n;
    cout << "Enter any Number" << endl;
    cin >> num;
    cout << "Enter n" <<  endl;
    cin >> n;
    cout << "sine of given x equals " << sin(num, n);
}
int factorial(int n){
    int a=1;
    for(int p=1; p<=n; p++){
        a=a*p;
    }
        return a;
}
double sin(double x, int n){
    double sine=0;
    for ( int a=1, b=1; a<n+n+1; a+=2, b++){
        sine=sine + (pow(-1, b+1))*(pow(x,a))/(factorial(a));
    }
    return sine;
}
double pow(double x, int n){
    double num=1;
    for (int a=1; a<=n; a++){
        num=num*x;
    }
    return num;
}

It uses taylor series to calculate sine. I also takes 'n' as number of terms to include from taylor series to improve accuracy. I have certain doubts in this

1) the sin function i created, I found by trial and error that in the for loop, I have to write 'a< n+n+1' , but If I tried to write 'a<2n+1' it gives me ugly compilation error. Why is it so? What can I do to make it that way?

2)If I try to enter large values of n (>15-16) it gives answer as 'nan'. Why is it so? I think double has huge capacity of storing numbers (10^408). Correct me If I'm wrong. Or what can be done to make it calculate for huge value of n.

3) I know the code I written is ugly, I don't want to use any library functions. Anyway what can I do to make this code do better in terms of algorithm. Is there any other efficient way without using any library.

4) Any other comments/hints/tips for learning more in future?

like image 991
Vaibhav Avatar asked Dec 05 '25 23:12

Vaibhav


2 Answers

  1. Use a < 2*n +1.

  2. factorial of 15 is 1,307,674,368,000. That number cannot be represented by an int. You have to rethink the code you use to compute the terms of the Taylor series for sine.

  3. It will be better to post your working code at http://codereview.stackexchange.com to get feedback on how to improve your code.

N-th term in the Taylor series expansion is (-1)^(N-1)x^(2*N+1)/(2*N+1)!
(N+1)-th term is (-1)^(N)x^(2*(N+1)+1)/(2*(N+1)+1)!

T(N+1) = -1*T(N)*x^2/((2*(N+1)+1)*(2*(N+1))

When you use this logic, you don't need to need pow or factorial. You derive the (N+t)-th term from the N-th term very easily. The starting point, T(0) is simply x.

Here's an example program:

#include <iostream>
#include <iomanip>

using namespace std;

double sin(double x, int n)
{
   double t = x;
   double sine = t;
   for ( int a=1; a<n; ++a)
   {
      double mult = -x*x/((2*a+1)*(2*a));
      t *= mult;
      sine += t;
   }
   return sine;
}

int main()
{
    double num;
    int n;
    cout << "Enter any Number" << endl;
    cin >> num;
    cout << "Enter n" <<  endl;
    cin >> n;
    cout << std::setprecision(20) << "sine of given x equals " << sin(num, n) << std::endl;

   return 0;
}

Sample input:

3.5
5

Output:

sine of given x equals -0.32838899588879211233

Sample input:

3.5
20

Output:

sine of given x equals -0.35078322768961955891

Sample input:

3.5
200

Output:

sine of given x equals -0.35078322768961955891

P.S. There is no change in output when N is changed from 20 to 200.

like image 100
R Sahu Avatar answered Dec 07 '25 15:12

R Sahu


1) You need to write

 a < 2*n+1 

You can use recursion functions to do the code more nice and clear.

like image 42
STF Avatar answered Dec 07 '25 14:12

STF



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!