本文目錄一覽:
用c語言計算sin(x)的近似值的代碼?
根據台勞公式:
sin(x)=x-(x^3)/3!+(x^5)/5!+……(-1)^(n)x^(2n+1)/(2n+1)!
採用遞推法根據級數的前20項計算sin(x)的近似值:
(注: x為弧度值, x^(n+1)表示x的n+1次方)
我寫的代碼如下:
# include stdio.h
int main ()
{
double sx,x,a,b;
int n,f=1;
printf (“Please input x:”);
scanf (“%lf”,x);
sx=a=x;
b=1;
for (n=1;n=20;++n)
{
a*=x*x;
b*=4*n*n+2*n;
f=-f;
sx+=a/b*f;
}
printf (“sin(x)=%lf\n”,sx);
return 0;
}
希望對你有所幫助。
C語言sin怎麼用
C語言sin()用來計算參數x 的正玄值,然後將結果返回。返回-1 至1 之間的計算結果。
例子:
#include math.h
main(){
double answer = sin(0.5);
printf(“sin(0.5) = %f\n”, answer);
}
執行
sin(0.5) = 0.479426
C語言sin():
sin()原型:double sin(double x)
sin()角度與弧度:
π=180°
1°=π/180
1(rad)=180/π
角度轉弧度:用角度乘以π/180
弧度轉角度:用弧度乘以180/π,或者用rtod()函數
擴展資料:
與sin相似的acos函數
函數名: acos
功 能:計算並返回arccos(x)值、要求-1=X=1
函數與形參類型:
double acos(x)
double x;
程序例:
#include stdio.h
#include math.h int main(void)
{
double result;
double x = 0.5; result = acos(x);
printf(“The arc cosine of %lf is %lf\n”, x, result);
return 0;
}
參考資料:CSDN博客頻道-C語言中sin和cos的用法
C語言 求sin值
首先,你的測試輸入和測試輸出的數據是對應不上的,你寫錯信息了!
輸入 3.1415026, 3 的時候,輸出才是 -0.07522 。
好了,正確的參考代碼如下:
#include stdio.h
double power(double x, int n); // 計算乘方的函數
double fact(int n); // 計算階乘的函數
int main(int argc, char const *argv[])
{
double x, s;
int n;
int sign = 1; //正負號開關變數,初始狀態為正
printf(“Please input a decimal number x , a postive int number n :\n”);
scanf(“%lf%d”, x, n);
for (int i = 0; i = n; i++)
{
s += sign * power(x, 2 * i + 1) / fact(2 * i + 1);
sign = -sign;
}
printf(“x = %g, n = %d, s = %.5lf \n”, x, n, s);
return 0;
}
//計算x^n
double power(double x, int n)
{
double p = 1;
// 這樣的循環條件,很簡潔。因為函數傳入的是形參,也不會對main的變數造成影響。
for (; n–;)
{
p *= x;
}
return p;
}
// 計算n!
double fact(int n)
{
double f = 1;
// 這樣的循環條件,很簡潔。因為函數傳入的是形參,也不會對main的變數造成影響。
for (; n;)
{
f *= n–;
}
return f;
}
測試截圖:(分別測試了角度為 180度、90度、45度的弧度值)
輸入和輸出語句,你不想要這麼多的提示信息的話,自己修改一下就可以了。
如有幫助,煩請點採納,謝謝!
c語言 計算sin
sin函數寫錯了,應該是: double sin…… { if… return 0; else { … … return sin(x,xx); } } 不要後面那個return z了
c語言中sin是啥?
C語言sin()用來計算參數x 的正玄值,然後將結果返回。返回-1 至1 之間的計算結果。
例子:
#include math.h
main(){
double answer = sin(0.5);
printf(“sin(0.5) = %f\n”, answer);
}
執行
sin(0.5) = 0.479426
C語言sin():
sin()原型:double sin(double x)
sin()角度與弧度:
π=180°
1°=π/180
1(rad)=180/π
角度轉弧度:用角度乘以π/180
弧度轉角度:用弧度乘以180/π,或者用rtod()函數
擴展資料:
與sin相似的acos函數
函數名: acos
功 能:計算並返回arccos(x)值、要求-1=X=1
函數與形參類型:
double acos(x)
double x;
程序例:
#include stdio.h
#include math.h int main(void)
{
double result;
double x = 0.5; result = acos(x);
printf(“The arc cosine of %lf is %lf\n”, x, result);
return 0;
}
參考資料:CSDN博客頻道-C語言中sin和cos的用法
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/219599.html