LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-28-2007, 07:24 PM   #1
darkangel29
Member
 
Registered: Nov 2004
Location: Puerto Rico
Distribution: Ubuntu 10.04
Posts: 121

Rep: Reputation: 15
help calculating sine using factorials equation


I write this code that all it does is calculate sine using the formula sin(x) = x − x^3/3! + x^5/5! - x^7/7! + x^9/9!...
Problem is when I look for the sine of 90 it tells me is 0.999408 instead of just one. How can I fix this??

Code:
#include <cstdlib>
#include <iostream>
#include <cmath>
#define PI 3.14159265358979323846
using namespace std;
int factorial(int);
void Sin(double &x,int n);

int main()
{
double angle=0;
double x=0;
cout<<"Input an angle and I will compute the sine of that angle:\n";
cin>>angle;

x=angle*(PI/180);

cout<<"sin("<<x<<") = ";
Sin(x,15);

return 0;
}
int factorial(int n)
{
if(n==0||n==1){
return 1;
}
else
return (n*factorial(n-1));
}
void Sin(double &x,int n)
{
double temp=0;
for(int i=0;i<n;i++){
temp+=(pow((double)(-1),(double)(i))*pow((double)(x),(double)(2*i+1)))/(double)(factorial(2*i+1));
}
cout<<temp<<endl;
}
 
Old 09-28-2007, 09:56 PM   #2
choogendyk
Senior Member
 
Registered: Aug 2007
Location: Massachusetts, USA
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,197

Rep: Reputation: 105Reputation: 105
Have you played with that n, which is 15 in Sin(x,15)?

What happens if you change it lower and higher? You could make a table of n vs estimate of Sin(x,n). Watch how it converges.

Aside from that, you're going to be getting deeper into computational and numerical methods, the cumulative effect of roundoff error, etc. You could take a course in that. Somewhere up in my attic I have a huge thick hardback book on Numerical Methods in Fortran. You could dig up something like that for C.
 
Old 09-28-2007, 10:39 PM   #3
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
This one gives sin(90°) = 1.0000000000000002:

Code:
double Sin(double x, int n)
{
   int    i;
   double term = x;
   double result = x;

   for (i = 3; i <= n; i += 2)
   {
      term *= -(x * x) / ((i - 1) * i);
      result += term;
   }

   return (result);
}
 
Old 09-29-2007, 12:20 PM   #4
darkangel29
Member
 
Registered: Nov 2004
Location: Puerto Rico
Distribution: Ubuntu 10.04
Posts: 121

Original Poster
Rep: Reputation: 15
Thanks a lot!!! Appreciate the help.
Now all I want to know is how do I round off numbers like 0.4999 to just 0.50?? I believe this can be done with the float command. Can somebody point me to the right direction? Thanks.

This is my new code
Code:
#include <cstdlib>
#include <iostream>
#include <cmath>
#define PI 3.141
using namespace std;
int factorial(int);
double Sin(double x,int n);

int main()
{
double angle=0;
double x=0, res_sin;
cout<<"Input an angle and I will compute the sine of that angle:\n";
cin>>angle;

x=angle*(PI/180);

cout<<"sin("<<x<<") = ";
res_sin=Sin(x,15);
cout<<res_sin<<endl;

return 0;
}
int factorial(int n)
{
if(n==0||n==1){
return 1;
}
else
return (n*factorial(n-1));
}
double Sin(double x, int n)
{
   int    i;
   double term = x;
   double result = x;

   for (i = 3; i <= n; i += 2)
   {
      term *= -(x * x) / ((i - 1) * i);
      result += term;
   }

   return (result);
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: The Linux Equation LXer Syndicated Linux News 0 08-14-2006 10:03 PM
Algebra Equation Software a_c_g_t Linux - Newbie 2 04-27-2006 08:46 AM
Transparency Equation leonidg Programming 3 02-08-2005 02:38 AM
an equation in c language liquid sky Programming 5 03-22-2004 02:25 PM
How to create equation in Abiword ? futurist General 9 04-07-2002 12:14 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration