c语言编写三角函数

Python026

c语言编写三角函数,第1张

求sin的:参考下 #include<stdio.h> void main() { double x,a,b,sum=0printf("请输入x的弧度值:\n")scanf("%lf",&x)int i,j,count=0for(i=1i+=2) { count++a=b=1for(j=1j<=ij++) { a*=xb*=(double)j} if(a/b<0.0000001) breakelse { if(count%2==0) sum-=a/belse sum+=a/b} } printf("%lf\n",sum)}

调用math.h中的三角函数,需要将角度值变换为弧度值,代码如下:#include<stdio.h>#include<math.h>#define PI 3.14159265359int main(){float st,a scanf("%f",&st) a = st * PI/180 printf("sin(st)=%f\n", sin(a)) printf("cos(st)=%f\n", cos(a)) return 0}

math.h里的三角函数用的单位是弧度,你貌似错在这里。 答案补充 Example

/* SINCOS.C: This program displays the sine, hyperbolic

* sine, cosine, and hyperbolic cosine of pi / 2.

*/

#include <math.h>

#include <stdio.h>

void main( void )

{

double pi = 3.1415926535

double x, y

x = pi / 2

y = sin( x )

printf( "sin( %f ) = %f\n", x, y )

y = sinh( x )

printf( "sinh( %f ) = %f\n",x, y )

y = cos( x )

printf( "cos( %f ) = %f\n", x, y )

y = cosh( x )

printf( "cosh( %f ) = %f\n",x, y )

} 答案补充 Output

sin( 1.570796 ) = 1.000000

sinh( 1.570796 ) = 2.301299

cos( 1.570796 ) = 0.000000

cosh( 1.570796 ) = 2.509178

Parameter

x

Angle in radians