3 Replies - 811 Views - Last Post: 12 February 2012 - 04:20 AM Rate Topic: -----

#1 ricardosms  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 73
  • View blog
  • Posts: 301
  • Joined: 02-April 10

Bug On Rounding On Divission And MOD Of Integers

Posted 11 February 2012 - 09:13 PM

Hello:

This is not a question it is a tip. It may help someone else.

I have a group of 25 labels named Label1, Label2, Label3, ... Label25 in a square of 5 per row, 5 per column and I wanted to assign the backcolor to all of them on a loop and assign a value of "True" or "False" to a matrix of 25 boolean values representing them.

But I was getting errors on the pattern created.

My approach was to use a substring containing a number copied from the label names:

   Num = Val(sender.name.substring(5))



And the subindexes of the matrix:
            p = (Num - 1) / 5
            q = (Num - 1) Mod 5




The values for the lower corner of the matrix were rouded to 5 instead of being 4. Instead of (4,0) ,(4,1), (4,2), (4,3) (4,4) I was getting (5,0),(5,1)...(5,5)

So I solved the problem by creating two single variables "A" and "B", doing the divission and MOD operations on them, finding the floor of them and then converting to integer:

    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _
        Label1.Click, Label2.Click, Label3.Click, Label4.Click, Label5.Click, _
        Label6.Click, Label7.Click, Label8.Click, Label9.Click, Label10.Click, _
        Label11.Click, Label12.Click, Label13.Click, Label14.Click, Label15.Click, _
        Label16.Click, Label17.Click, Label18.Click, Label19.Click, Label20.Click, _
        Label21.Click, Label22.Click, Label23.Click, Label24.Click, Label25.Click

        If cbManual.Checked = True Then
            Dim A, B As Single
            Dim Num, p, q As Integer
            'Avoid Problem Of Rounding Up
            Num = Val(sender.name.substring(5))
            A = Math.Floor((Num - 1) / 5)
            p = CInt(A)
            B = Math.Floor((Num - 1) Mod 5)
            q = CInt(B)/>

            If sender.backcolor = Color.Black Then
                sender.backcolor = Color.Red
                PX(p, q) = True
            Else
                sender.backcolor = Color.Black
                PX(p, q) = False
            End If

        End If
    End Sub




This may be a bad side effect if you are wanting to truncate a number. Don't trust all the operations, do a side check. You will avoid getting some bugs.

I hope this helps someone.
ricardosms.

Is This A Good Question/Topic? 0
  • +

Replies To: Bug On Rounding On Divission And MOD Of Integers

#2 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Bug On Rounding On Divission And MOD Of Integers

Posted 11 February 2012 - 09:19 PM

Heya!
This might be better as a blog post then a mod could feature it.
But great tip.
J-Bo
Was This Post Helpful? 0
  • +
  • -

#3 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,468
  • Joined: 29-May 08

Re: Bug On Rounding On Divission And MOD Of Integers

Posted 12 February 2012 - 01:10 AM

It bug in your code, not in the framework.

/ is floating point divide, which then to round up.
\ is integer divide.

Try setting Option Strict On, which forces you to use explicit casting, rather than implicit casting.

Which has the effect in your code, on casting the integers into doubles before doing the division, then cast back (losing precision) into an Integer.

Math.Round as some overloads where you can control to some extent the direction of the rounding.
Math.Truncate return the Integral part of floating point number.

This post has been edited by AdamSpeight2008: 12 February 2012 - 01:16 AM

Was This Post Helpful? 1
  • +
  • -

#4 ricardosms  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 73
  • View blog
  • Posts: 301
  • Joined: 02-April 10

Re: Bug On Rounding On Divission And MOD Of Integers

Posted 12 February 2012 - 04:20 AM

Thank you,AdamSpeight2008

I did't know that existed. For me a divission was a divission and I always assumed it was just to provide for people that is used to other systems. Like on the web pages links that you use backslash and forward slash indistintively.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1