本文主要是介绍泰勒法计算sin和cos值,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
#include <stdio.h>
#define pie 3.14
double myabs(double n);
double mysin(double x);
double mycos(double x);
main()
{ int choose; double result,jiaodu; while(1) { printf("************\n"); printf("sin运算请选1\n"); printf("cos运算请选2\n"); printf("要退出请按0!\n"); printf("************\n"); scanf("%d",&choose); if (choose==0) break; switch(choose) { case 1: { printf("请输入要计算sin角度值\n"); scanf("%lf",&jiaodu); result=mysin(jiaodu*pie/180); break; } case 2: { printf("请输入要计算cos角度值\n"); scanf("%lf",&jiaodu); result=mycos(jiaodu*pie/180); break; } } printf("计算结果为:%f\n",result); }
}
double myabs(double n)
{ if (n<0) n*=(-1); return (n);
}
double mysin(double x)
{ int i=1,sign=1; double item=x,frac=0,fz=x,fm=1; for (;myabs(item)>=10E-5;i+=2) { frac+=item; fz=fz*x*x; fm=fm*(i+1)*(i+2); sign=-sign; item=sign*(fz/fm); } frac+=item; return (frac);
}
double mycos(double x)
{ int i=0,sign=1; double item=1,frac=0,fz=1,fm=1; for (;myabs(item)>=10E-5;i+=2) { frac+=item; fz=fz*x*x; fm=fm*(i+1)*(i+2); sign=-sign; item=sign*(fz/fm); } frac+=item; return (frac);
}
这篇关于泰勒法计算sin和cos值的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!