Answer the question
In order to leave comments, you need to log in
Si. Which option calculates the most accurate value?
Solved the problem:
int _tmain(int argc, _TCHAR* argv[])
{ int i,z;
float x,s,n,q,y,m;
s=0;n=0;z=1;
for(i=0;i<10000;i++)
{
n=n+1.0;
x=1.0/(n*z);
s=s+x;
z=z*(-1);
}
cout<<s<<"\n";
//Б
s=0; n=2.0; x=0; y=0; m=1.0; q=0;
for (i = 1; i<5000; i++)
{
//чётные знаменатели
x = 1.0 / n;
s = s + x;
n = n + 2.0;
//нечётные знаменатели
y=1.0/m;
q=q+y;
m=m+2.0;
}
s = q - s;
cout << s<<"\n";
//В
s = 0; n = 10000; x=0; z = (-1);
for (i = 10000; i>0; i--)
{
x = (1.0 / n)*z;
s = s + x;
n = n - 1.0;
z = z * (-1);
}
cout << s << "\n";
//Г
s = 0; n = 10000; x=0; q=0; y=0; m=9999;
for (i = 5000; i>0; i--)
{
x = 1.0 / n;
s = s + x;
n = n - 2.0;
y = 1.0 / m;
q = q + y;
m = m - 2.0;
}
s = q - s;
cout << s <<"\n";
return 0;
}
Answer the question
In order to leave comments, you need to log in
"Remember that not all decimal numbers have a binary floating point representation. For example, the number "0.2" would be represented as "0.200000003" in single precision. Accordingly, "0.2 + 0.2 ≈ 0.4" The absolute error in a particular case may not be high, but if we use such a constant in a cycle, we can get the accumulated error."
(C) What you need to know about floating point arithmetic
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question