ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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;
}
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,197
Rep:
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.
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);
}
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);
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.