Why does the compilation in the next step after "-0.1" go "-1.38 ...", and then "0.999 ..."?

When compiling

using System; namespace OOP_Lab_3 { class Program { static void Main(string[] args) { double x1, x2, step, eps, amount, func, x, value; int n; //Ввод данных пользователем Console.WriteLine("Eps= "); eps = 0.0001; // Convert.ToDouble(Console.ReadLine()); Console.WriteLine("x begin= "); x1 = -1; //Convert.ToDouble(Console.ReadLine()); Console.WriteLine("x end= "); x2 = 0.9; // Convert.ToDouble(Console.ReadLine()); Console.WriteLine("Step= "); step = 0.1; // Convert.ToDouble(Console.ReadLine()); Console.WriteLine(); for (x = x1; x <= x2; x += step) { n = 1;// value = eps + 1; value = 1; amount = 0; while (eps <= Math.Abs(value)) { value = -Math.Pow(x, n) / n; amount += value; n++; } func = Math.Log(1 - x); //Функция //вывод Console.WriteLine("\tx= " + x + "\ty= " + amount + "\tn= " + n + "\t auto= " + (Math.Round(func, 10))); Console.WriteLine(); } Console.ReadKey(); Console.ReadKey(); } } } 

    2 answers 2

    Because there are no exact floating point calculations - rounding errors constantly accumulate. Please note: there is an error of only 10 to the power of -16, this is very little!

    I would advise you to output x in F1 format, and y - in F4 format:

     Console.WriteLine("\tx={0:F1}\ty={1:F4}\tn={2}\tauto={3:F4}", x, y, n, func); 

      If you want accurate calculations, then use Wolfram Mathematica, and fully agree with the previous answer. Simply, the info is stored in the 2 number system (s \ s) and for not 0.1 in 10 s / s it is not a fact that it will be simple, perhaps periodic as 1/3 in 10 s / s, this is where accuracy is lost. Perhaps not only here.