R
R
royalfest522018-05-29 20:50:48
C++ / C#
royalfest52, 2018-05-29 20:50:48

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;
}

It counts correctly, everything works, but you need to determine which of the calculation options has the highest calculation accuracy, I don’t understand how to do this at all, please help me.
And I also need to answer the question - Why are different results obtained in these options? Please let me know what's wrong, thanks in advance!
And I am attaching the text of the task itself for clarity:
5b0d92e9d3bbe345814223.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman, 2018-05-29
@royalfest52

long double instead of float

spoiler
и язык С++ в Cи нет cout

M
Mikhail Kuzminov, 2018-05-29
@mkuzminov

"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 question

Ask a Question

731 491 924 answers to any question