Home > Back-end >  To bosses guidance; using the following formula for cos (x) approximation, and accurate to the last
To bosses guidance; using the following formula for cos (x) approximation, and accurate to the last

Time:12-01

Topics are as follows:


The code below
 
# include & lt; Stdio. H>
# include & lt; Math. H>

Double funcos (double e, double x);

Int main ()
{
Double e, x;

Lf the scanf (" % % lf ", & amp; E, & amp; X);
Printf (" cos (. % 2 f)=%. 6 f \ n ", x, funcos (e, x));

return 0;
}
Double funcos (double e, double x)
{
Double t=1.0;
Double l=1.0;
Int I;
Int j=2;
Double the total;
Double sum;
While ((t/l) & gt; E)//when does not meet the precision cycle
{
T=pow (x, j);
For (I=1; I & lt;=j; I++)
{
L *=I;//for molecular
}

Total +=- (t/l);//a single

The sum +=total;

J +=2;//increment index + 2
}
Return the sum + 1;
}


I think that no problem, but the code is wrong, where I went wrong, don't see


CodePudding user response:

Your custom function, total and sum uninitialized,

In addition, the output of the double should use % lf, should be a % of the building Lord. 2 lf

CodePudding user response:

refer to the original poster mountains roll also thinks response:
title as follows:


The code below
 
# include & lt; Stdio. H>
# include & lt; Math. H>

Double funcos (double e, double x);

Int main ()
{
Double e, x;

Lf the scanf (" % % lf ", & amp; E, & amp; X);
Printf (" cos (. % 2 f)=%. 6 f \ n ", x, funcos (e, x));

return 0;
}
Double funcos (double e, double x)
{
Double t=1.0;
Double l=1.0;
Int I;
Int j=2;
Double the total;
Double sum;
While ((t/l) & gt; E)//when does not meet the precision cycle
{
T=pow (x, j);
For (I=1; I & lt;=j; I++)
{
L *=I;//for molecular
}

Total +=- (t/l);//a single

The sum +=total;

J +=2;//increment index + 2
}
Return the sum + 1;
}


I think that no problem, but the code is wrong, where I went wrong, don't see


This single comment of one line of what you mean?
You see your problem, plus or minus a didn't control well, how could the answer right?
Positive and negative items with *=1 (that is, by 1) control, so change to total=1.0 (initial value), the sum=0
Then a for loop in the
Total +=- (t/l); The sum +=total; To sum +=total * (t/l); Total *=1;//total as a flag of plus or minus sign, every operation change a symbol

CodePudding user response:

 double funcos (double e, double x) 
{
Double k=1.0;
Double s;
Double total=1;
Int j;
Double I=2;
Int falg=1;
Int sum=1;

While (k & gt;=e)
{
S=pow (x, I);

For (j=1; J & lt;=I; J++)
{
The sum *=j;

}
K=s/sum;
Total=total + k * falg;
Falg=- falg;

I +=2;
}
Return the total;
}

Changed or not

CodePudding user response:

Every time you check sum factorial right well, calculating factorial initialized again?
Or you could get rid in the for j=1, on the outside to initialize the j; Or before each for j loop initialization sum
In addition, while (k>=e) also has a problem, assuming that k=s/sum is less than e, the back of the totle=totle + k * flag or performed, which is going to calculate a,
  • Related