This question has already been answered:

Faced with a little misunderstanding. Why on the sixth iteration of the loop does garbage get into the iterator (-5.55111 ..)? A must be 0. Program code:

using System; namespace frost1 { class Program { static void Main() { double a, b, h; Console.WriteLine("Input please the A variable (bottom):"); a = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("Input please the B variable (top):"); b = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("Input please the H (step) variable:"); h = Convert.ToDouble(Console.ReadLine()); if (a >= b) Console.WriteLine("Wrong interval... Repeat the input"); else { double res = 0; Console.WriteLine("x\t\t\ty"); for (double i = a; i <= b; i+=h) { res = Math.Pow(i, 2) - Math.Sin(Math.Pow(i,4)); Console.WriteLine(i+"\t\t\t"+res); } } } } } 

Screenshot:

enter image description here

Reported as a duplicate member by VladD c # Jun 22 '16 at 11:18 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • Solved the problem using the decimal type. - Dmitry Shulga
  • 3
    Please note that you have not -5.55111 , but -0.0000000000000000555111 . - VladD

1 answer 1

Because you need to round off. The reason for this is the format for representing floating-point numbers in a computer. You can read here .

In your case, there are problems with the accuracy of representing values ​​near zero. You can try this:

 ... for (double i = a; i <= b; i+=h) { res = Math.Pow(i, 2) - Math.Sin(Math.Pow(i,4)); Console.WriteLine(Math.Round(i, 1)+"\t\t\t"+Math.Round(res, 4)); }