Home > Back-end >  Calculating the value of sinx
Calculating the value of sinx

Time:04-08

Known sinx approximate computation formula is as follows:

Sin x=x - 3 x3/! + x5/5! - 7 x7/! +... X2n + (1) n - 1-1/(2 n - 1)!

Where x as the radian, n positive integers, write programs based on user input x and n value, using the approximate calculation formula of the sinx approximation, the required output 8-bit decimal,

[form] input

From the console input decimal x (0 & lt;=x<=20) and an integer n (1 & lt;=n<=5000), two Numbers separated by a space,

[] output form

Results: the console output formula 8-bit decimal,

[1] sample input

0.5236 4
[1] sample output

0.50000105
[sample input 2]

0.5236 50
[2] sample output

0.50000106
[example]

Input x 0.5236, n is 4, sinx approximate calculation formula of the value of 0.50000105, 8-bit decimal; Also, input x 0.5236, n is 50, approximate calculation formula of sinx values, a value of 0.50000106, 8-bit decimal,

Note: in order to ensure the accuracy of the data and consistency, please use the double data type save calculation results,

CodePudding user response:

Reference:
 # include 
# include

Double fac (int n);
Double sinx (double radian, int n);

Int main (int arg c, char * argv [])
{
Double radian;
Int n;
Lf the scanf (" % % d ", & amp; Radian, & amp; N);
Printf (" %. 8 f \ n ", sinx (radian, n));

return 0;
}


Double fac (int n)
{
Double num=1;
for(int j=1; J<=n; J++)
Num=num * j;
Return num.
}

Double sinx (double radian, int n)
{
Double x=radian;
Int I=2 and k=0, j=1;
Double temp, result=0.0;
Do {
K=2 * j - 1;
Temp (1, I) *=pow pow (x, k)/fac (k);
Result=result + temp;
i++;
J++;
} while (j<=n);
return result;
}
  • Related