当前位置: 代码迷 >> C语言 >> 做算y=sinx的程序,不能用库函数
  详细解决方案

做算y=sinx的程序,不能用库函数

热度:129   发布时间:2006-03-01 23:28:00.0
做算y=sinx的程序,不能用库函数
如题
刚学c语言,不熟悉运用操作和算法,望大家帮忙

[此贴子已经被作者于2006-3-1 23:28:15编辑过]

搜索更多相关的解决方案: sinx  函数  c语言  算法  

----------------解决方案--------------------------------------------------------
[IMG]http://www.wikilib.com/images/math/ef491e731c806ce36b37ddb84445ae22.png[/IMG]


----------------解决方案--------------------------------------------------------
[IMG]http://www.wikilib.com/images/math/6cd3737be7a2653367595e4365ba58f9.png[/IMG]


----------------解决方案--------------------------------------------------------
用循环控制精度
----------------解决方案--------------------------------------------------------

compiled to void, faint 'n pfft.

#include <stdio.h>

main()
{double sinx();
long int prod();
long int cf();
float x;
scanf("%f",&x);
printf("sin(%f)=%f",x,sinx(x));
}

/*阶乘函数*/
long int prod(int c)
{ long int b;
if (c<=1) return (1);
{ b=(c+1)*prod(c--);
return b;
}
}
/*指数函数*/
long int cf(int a, int b)
{ int i;
long int c=a;
for (i=0;i<b;i++)
c=c*a;
return c;
}

/*sin()函数*/
double sinx(float y)
{
int n=0,m=0;
double d=6,z=0,x=0;
for (n=0; d>0.001; n++)
{m=2*n+1;
d=1/prod(m);
x=cf(y,m);
z=z+(cf(-1,n)/d)*x;
}
return z;
}

[此贴子已经被作者于2006-3-2 11:18:58编辑过]


----------------解决方案--------------------------------------------------------
#include <stdio.h>
#include <conio.h>
#include <math.h>
#define MIN 0.0000001 /*精度由这个控制*/
main()
{
int flag=1;
long n=1;
double sum=0,temp=0;
clrscr();
do
{
temp=sum;
sum+=1.0*flag*4/n;
flag=-flag;
n+=2;
}while(fabs(temp-sum)>MIN);
printf("n=%ld\npi=%.7lf\n",n,sum);
getch();
}


我算到小数点后7位,结果是3.1415927
----------------解决方案--------------------------------------------------------
以下是引用cordier在2006-3-2 22:22:00的发言:
#include <stdio.h>
#include <conio.h>
#include <math.h>
#define MIN 0.0000001 /*精度由这个控制*/
main()
{
int flag=1;
long n=1;
double sum=0,temp=0;
clrscr();
do
{
temp=sum;
sum+=1.0*flag*4/n;
flag=-flag;
n+=2;
}while(fabs(temp-sum)>MIN);
printf("n=%ld\npi=%.7lf\n",n,sum);
getch();
}


我算到小数点后7位,结果是3.1415927

老大,谁让你算圆周率的值了


----------------解决方案--------------------------------------------------------
放错地方了。
----------------解决方案--------------------------------------------------------
#include <stdio.h>
#include <conio.h>
#include <math.h>
#define MIN 0.0001
int main()
{
int flag=1;
long n=1;
double x,sum=0,temp,fuction=1,powx=1;
printf("Please input X:");
scanf("%lf",&x);
powx=x;
do
{
temp=sum;
sum+=1.0*flag/fuction*powx;
n+=2;
fuction*=n*(n-1);
powx*=x*x;
flag=-flag;
}while(fabs(sum-temp)>MIN);
printf("sum=%.6lf\nmath sin=%.6lf\n",sum,sin(x));
getch();
return 0;
}

----------------解决方案--------------------------------------------------------
上面输入的x是弧度不是角度。
我试了一下2rad。输出结果是
sum=0.909296
调用系统的sin是0.909297

----------------解决方案--------------------------------------------------------
  相关解决方案