重慶分公司,新征程啟航
為企業(yè)提供網(wǎng)站建設(shè)、域名注冊(cè)、服務(wù)器等服務(wù)
為企業(yè)提供網(wǎng)站建設(shè)、域名注冊(cè)、服務(wù)器等服務(wù)
調(diào)用math.h中的三角函數(shù),需要將角度值變換為弧度值,代碼如下:
望奎網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),望奎網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為望奎數(shù)千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站建設(shè)要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的望奎做網(wǎng)站的公司定做!
#includestdio.h
#includemath.h
#define PI 3.14159265359
int 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;
}
要用弧度計(jì)算的,另外,pintf語(yǔ)句中,應(yīng)該是"%lf",不是"f%"
sin()是三角函數(shù),參數(shù)使用的是弧度,不是度。
asin()才是反三角函數(shù)。
資料 :
NAME
asin, asinf, asinl - arc sine function
SYNOPSIS
#include math.h
double asin(double x);
float asinf(float x);
long double asinl(long double x);
Link with -lm.
DESCRIPTION
The asin() function calculates the arc sine of x; that is the value
whose sine is x. If x falls outside the range -1 to 1, asin() fails
and errno is set.
RETURN VALUE
The asin() function returns the arc sine in radians and the value is
mathematically defined to be between -PI/2 and PI/2 (inclusive).
math.h里的三角函數(shù)用的單位是弧度,你貌似錯(cuò)在這里。 答案補(bǔ)充 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 );
} 答案補(bǔ)充 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
1.
C語(yǔ)言的三角函數(shù)庫(kù)采用的單位都是弧度,如果要使用角度,就必須轉(zhuǎn)換,從角度轉(zhuǎn)換成弧度,或者是重寫一個(gè)三角函數(shù)庫(kù)。
2.
方法一,在調(diào)用三角函數(shù)之前先把角度換算成弧度,調(diào)用反三角函數(shù)之后把弧度換算成角度就可以了。可以用
pi
=
4.0
*
atan(1)
算出pi,用
a
=
d
/180.0*pi
轉(zhuǎn)換角度到弧度。
例如:
sin(45
/180.0*pi);
就是計(jì)算的sin45。
3.
方法二,直接覆寫三角函數(shù)。
例如sin函數(shù):
double
dsin(double
d){
return
sin(45
/180.0*pi);
//原理和方法一樣,調(diào)用的時(shí)候直接使用dsin(45)即可
}
求sin的:參考下 #includestdio.h void main() { double x,a,b,sum=0; printf("請(qǐng)輸入x的弧度值:\n"); scanf("%lf",x); int i,j,count=0; for(i=1;;i+=2) { count++; a=b=1; for(j=1;j=i;j++) { a*=x; b*=(double)j; } if(a/b0.0000001) break; else { if(count%2==0) sum-=a/b; else sum+=a/b; } } printf("%lf\n",sum); }