# Bug On Rounding On Divission And MOD Of Integers

Page 1 of 1

## 3 Replies - 1525 Views - Last Post: 12 February 2012 - 04:20 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=266346&amp;s=f89c6ce9dbaac3a102508cc8bf43444e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ricardosms

• D.I.C Regular

Reputation: 74
• 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

• vexing conundrum

Reputation: 587
• Posts: 2,746
• 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

• MrCupOfT

Reputation: 2298
• Posts: 9,535
• 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

### #4 ricardosms

• D.I.C Regular

Reputation: 74
• Posts: 301
• Joined: 02-April 10

## Re: Bug On Rounding On Divission And MOD Of Integers

Posted 12 February 2012 - 04:20 AM