0

I have to generate a sequence of number equally spaced one to another. I have the lower bound, the upper bound and the step from one to another. To this purpose I wrote a Do-Loop clause. If I try to do this with few numbers as output (the y-case in the provided code), it works fine, but when the numbers are more (the z-case in the code below), my software freezes. Here is the code I've provided in VS2019:

    Dim y As Double = DataGridView2.Rows(0).Cells(2).Value
    Dim z As Double = DataGridView2.Rows(0).Cells(4).Value
    Do
        ListBox8.Items.Add(y)
        y += CDbl(Form1.TextBox2.Text)
    Loop Until y = DataGridView2.Rows(0).Cells(3).Value
    ListBox8.Items.Add(y)
    Do
        ListBox9.Items.Add(z)
        z += CDbl(Form1.TextBox2.Text)
    Loop Until z = DataGridView2.Rows(0).Cells(5).Value
    ListBox9.Items.Add(z)

For the case I'm trying to make working, the y-case has 4 numbers as output, instead the z-case should provide 61 numbers as output. How can I solve this issue? Thanks all are gonna answer me.

Best regards

  • Side Note: [What do Option Strict and Option Explicit do?](https://stackoverflow.com/q/2454552/14171304). – dr.null Mar 04 '21 at 17:24
  • You're working with doubles, so it's possible that `z = DataGridView2.Rows(0).Cells(5).Value` is never true due to precision and rounding errors. Give some examples of lower/upper bound, steps, and the value in your DataGridView. – Idle_Mind Mar 04 '21 at 17:29
  • @Idle_Mind in this case the lower bound is 0, the upper is 3 and the step is 0.05. But it could vary and the number of the upper bound can also be much higher. I've also tried a single precision number with no changes – telemaco10399 Mar 04 '21 at 17:40
  • What Every Computer Scientist Should Know About Floating Point Arithmetic: https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html – Craig Mar 04 '21 at 21:08
  • Bruce Dawson's collected articles on floating point math: https://randomascii.wordpress.com/category/floating-point/page/3/ – Craig Mar 04 '21 at 21:10
  • The fundamental problem here is that adding steps of 0.05 isn't going to end up at a whole number due to round-off errors, because 0.05 is not exactly representable in binary floating point. Looping like this may be more accurate if you multiply by the index instead of adding the increment on each step, but you could still end up with round-off issues depending on your target. – Craig Mar 04 '21 at 21:12

2 Answers2

0

You should avoid manipulating the GUI in loops. It's extremely inefficient. You should create a list of Integer and bind this list to your DataGridView or ListBox or whatever.

Try something like:

List<Double> data = new List<Double>():
data.Add(1.0); // add as many as you want
listbox.DataSource=data

Lookup data binding examples on the net. I am on my mobile right now. If it does not work out let me know and I will provide working code.

You will probably never be in a position where z is exactly equal to the value in the gridview cell. This is because of floating point rounding error. As such should be:

Loop Until Math.Abs(z - target) < epsilon

target is your cell content. epsilon is a small number such as 0.001.

Tarik
  • 10,810
  • 2
  • 26
  • 40
  • Could you explain me how to add data? I can't understand how could I tell the software to stop filling the list when convergence is reached. – telemaco10399 Mar 04 '21 at 17:44
0

You could change it up to:

While z <= DataGridView2.Rows(0).Cells(5).Value
    ListBox9.Items.Add(z)
    z += CDbl(Form1.TextBox2.Text)
End While

This way it can't get stuck in an infinite loop looking for an exact match.

Idle_Mind
  • 38,363
  • 3
  • 29
  • 40