
Apr 8th, 2013, 04:06 PM
#1
Thread Starter
Addicted Member
mod math question
This makes no sense to me. The mod fuction is suppost to return the remainder of a division problem. It isn't for me. I have tried just about every combination of dim'ing the variables as doubles or integers, all to no result. Why does this not work?
Dim numberOfSamples As Integer
Dim modNumb As Integer
Dim modAmount As Double
modNumb = 4
numberOfSamples = 41
modAmount = numberOfSamples / modNumb Mod modNumb
'modAmount = 2.25 one time 2.2 another, and 2 a third time depending on how the variables were defined; why is this not 1 like it should be????

Apr 8th, 2013, 04:15 PM
#2
Re: mod math question
41/4 = 10.25 Mod 4 = 2.25
As the 6dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables"  jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

Apr 8th, 2013, 04:24 PM
#3
Thread Starter
Addicted Member
Re: mod math question
no it doesn't, 41/4 = 10 remainder 1, because 4 * 10 + 1 = 41. thus the mod should return 1 not 2.25

Apr 8th, 2013, 04:31 PM
#4
Re: mod math question
Really? The computer and I get exactly the same answer by exactly the same method and we are in the wrong? Your equation is (41/4) Mod 4. The answer you want is given by the equation 41 Mod 4
As the 6dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables"  jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

Apr 8th, 2013, 04:34 PM
#5
Re: mod math question
That's not how the Mod works. Mod is an operator just the same as +, , *, / and \. It is a bit unfortunate that it is the only math operator that isn't a symbol, but that's life. What you actually appear to want is just this:
41 Mod 4, which will equal 1.
Perhaps this will make it more clear:
41 + 4 = 45
41  4 = 41
41 / 4 = 10.25
41 * 4 = 164
41 \ 4 = 10
41 Mod 4 = 1
EDIT: I thought I might be too slow on that one.
My usual boring signature: Nothing

Apr 8th, 2013, 04:36 PM
#6
Re: mod math question
So break up the statement so that you can see what is going on, and you will find that dun is correct.
Code:
Dim numberOfSamples As Integer
Dim modNumb As Integer
Dim modAmount As Double
modNumb = 4
numberOfSamples = 41
modAmount = numberOfSamples / modNumb
Stop
modAmount = modAmount Mod modNumb
Stop

Apr 8th, 2013, 04:41 PM
#7
Thread Starter
Addicted Member
Re: mod math question
According to wikipedia, and my mathematics degree (not trying to be a wise guy here), http://en.wikipedia.org/wiki/Modulo_operation
Consider another example(straight from the wikipedia site):
the expression "5 mod 2" would evaluate to 1 because 5 divided by 2 leaves a quotient of 2 and a remainder of 1
thus the mod function returns the remainder. Punch my problem into a calculator, you get 41/4 = 10.25 to convert that to a mixed number you get 10 1/4, which mean a quotient of 10 and a remainder of 1, thus as I said the mod function should return 1 not 2.25.
algebraicly, you would have this, assuming you have a number i and you want to return the remainder after dividing i by j and put the result in k (think k=i mod j)
k = i  ( j * int ( i / j ) )
I realize that 2.25 is what modAmount = numberOfSamples / modNumb
is evaluating too, what I am saying is that it is wrong.

Apr 8th, 2013, 04:49 PM
#8
Re: mod math question
That's true. If you did 41 Mod 4, you would get 1, which is exactly what you are saying. What you actually did, though, was (41/4) Mod 4, which does not equal 1.
The mod is returning the remainder from the division. What you are trying to do is do the division....then apply the mod to the result. The only way your solution could make sense is if you believed that the equation 10/4 returned some value that remembered the result AND the remainder such that you could later ask for the remainder. That's not what it is doing. Mod is just performing the division and returning the remainder, just as / and \ are just performing the division and returning the result.
EDIT: Actually, I was replying to only the first part. To reply to the final statement I would say that the mistake is that you are expecting Mod to be a function that acts on a previous division to return the remainder. In fact, Mod is not a function, but an operator that performs the division and returns the remainder. You are thinking of Mod as talking about some other operation, but it is an operation all on its own.
My usual boring signature: Nothing

Apr 8th, 2013, 04:51 PM
#9
Thread Starter
Addicted Member
Re: mod math question
I feel like a real dork. Please note: I have a degree in mathematics, not computer science programming! HA HA that is funny stuff. Got it thanks!!!! Shaggy Hiker wins the award for this thread!!!!
adam

Apr 8th, 2013, 04:53 PM
#10
Re: mod math question
Guess I didn't need to add the edit then...
My usual boring signature: Nothing

Apr 8th, 2013, 04:54 PM
#11
Re: mod math question
How truly it is said of mathematicians that their only problem is that they can't do arithmetic!
As the 6dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables"  jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

Apr 8th, 2013, 04:54 PM
#12
Hyperactive Member
Re: mod math question
Originally Posted by Adam Stallcup
According to wikipedia, and my mathematics degree (not trying to be a wise guy here), http://en.wikipedia.org/wiki/Modulo_operation
Consider another example(straight from the wikipedia site):
the expression "5 mod 2" would evaluate to 1 because 5 divided by 2 leaves a quotient of 2 and a remainder of 1
thus the mod function returns the remainder. Punch my problem into a calculator, you get 41/4 = 10.25 to convert that to a mixed number you get 10 1/4, which mean a quotient of 10 and a remainder of 1, thus as I said the mod function should return 1 not 2.25.
algebraicly, you would have this, assuming you have a number i and you want to return the remainder after dividing i by j and put the result in k (think k=i mod j)
k = i  ( j * int ( i / j ) )
I realize that 2.25 is what modAmount = numberOfSamples / modNumb
is evaluating too, what I am saying is that it is wrong.
The reason you are getting 2 or 2.5 depending on your variable declarations is because you are actually performing a Mod on the remainder of 41 / 4.
41 / 4 = 10.25 (or 10 if it is an integer)
10.25 Mod 4 will result in a quotient of 2 with a remainder of 2.25
if you want your result to be 1 then you need to perform 41 Mod 4 because that will give a quotient of 10 with a remainder of 1

Apr 8th, 2013, 05:19 PM
#13
Thread Starter
Addicted Member
Re: mod math question
All right Dunn, that was a bit below the belt!! ha ha
Ok, so after my small situation of momentatary brain processing loss, I have this:
Dim numberOfSamples As Integer
Dim modNumb As Integer
Dim modAmount As Integer
Dim division As Integer
modNumb = CInt(modNumber.Text)
numberOfSamples = CInt(numSamps.Text)
modAmount = numberOfSamples Mod modNumb
If modAmount = 0 Then
division = numberOfSamples / modNumb
TextBox1.Text = division
End If
If modAmount = 1 Then
division = numberOfSamples / modNumb
TextBox1.Text = division + 1
End If
If modAmount = 2 Then
division = numberOfSamples / modNumb
TextBox1.Text = division + 1
End If
If modAmount = 3 Then
division = numberOfSamples / modNumb
TextBox1.Text = division
End If
I want to print 4 samples per page, and that is why I have an if statement for each mod value 0, 1, 2 and 3. But sometimes it works, othertimes not. For instance: 22 samples should print on 5 full pages, and 1 page with 2 on it, but according to the results I get for 22, it is 7 pages, not 6;
My thought was this: integer = integer / integer, will result in the integer rounded, so if the answer was actually 6.2, the integer part would be 6; likewise if the answer was 6.7, then my integer would be 7 because of rounding. Is this how it works? because sometimes it looks like with the mod 2 example I have to add 1 to get the correct number of pages, and other times I don't need too. Try it with 26 samples: 26 mod 4 is 2, and it tells me that I need 7 pages, which is correct with 6 pages of 4 samples, and 2 on the 7th page. This is confusing.
adam

Apr 8th, 2013, 05:35 PM
#14
Re: mod math question
try this:
Code:
Dim numberOfSamples As Integer
Dim modNumb As Integer
Dim modAmount As Integer
Dim division As Integer
modNumb = CInt(modNumber.Text)
numberOfSamples = CInt(numSamps.Text)
modAmount = numberOfSamples Mod modNumb
division = numberOfSamples \ modNumb 'Note the change from / (division) to \ (integer division)
' alternate method:
' division = (numberOfSamples  modAmount) \ modNumb 'Remove the excess, then a simple divide will go evenly...
' Division seemed to be Inc'd when mod = 1 or 2...
If modAmount = 1 orElse modAmount = 2 then
division += 1
End If
'If modAmount = 3 Then
' division = numberOfSamples / modNumb
' was there a reason Disivion isn't incremented when mod = 3?
' TextBox1.Text = division
'End If
TextBox1.Text = division
tg

Apr 8th, 2013, 05:41 PM
#15
Re: mod math question
So the real question was to calculate the number of pages based upon the total number of samples and the number of samples per page.
Here is something that should help
Code:
Dim numberOfSamples As Integer
Dim samplesPerPage As Integer = 4
Dim numFullPages As Integer
Dim lastPageItems As Integer
For numberOfSamples = 3 To 36 Step 3
numFullPages = numberOfSamples \ samplesPerPage
lastPageItems = numberOfSamples  (numFullPages * samplesPerPage)
Debug.WriteLine(String.Format("Number of samples {0}, Full pages {1}, Last Page {2}", _
numberOfSamples, numFullPages, lastPageItems))
Stop
Next

Apr 8th, 2013, 05:54 PM
#16
Re: mod math question
All right Dunn, that was a bit below the belt!!
It's true of all of us. It's just one of those things. Delving into the higher reaches of any field of study inevitably renders you incapable of thinking small! There was a film made some years ago of recent physics graduates showing that the great majority of them when presented with a wire, a battery, and a light bulb and told to let there be light took several minutes to work it out or simply failed to do it all! Isn't it encouraging to know that the guys at CERN who make miniature black holes every day probably couldn't change a fuse. NO, wait, what am I saying.
As the 6dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables"  jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

Apr 8th, 2013, 10:24 PM
#17
Re: mod math question
Originally Posted by Adam Stallcup
All right Dunn, that was a bit below the belt!! ha ha
Ok, so after my small situation of momentatary brain processing loss, I have this:
Dim numberOfSamples As Integer
Dim modNumb As Integer
Dim modAmount As Integer
Dim division As Integer
modNumb = CInt(modNumber.Text)
numberOfSamples = CInt(numSamps.Text)
modAmount = numberOfSamples Mod modNumb
If modAmount = 0 Then
division = numberOfSamples / modNumb
TextBox1.Text = division
End If
If modAmount = 1 Then
division = numberOfSamples / modNumb
TextBox1.Text = division + 1
End If
If modAmount = 2 Then
division = numberOfSamples / modNumb
TextBox1.Text = division + 1
End If
If modAmount = 3 Then
division = numberOfSamples / modNumb
TextBox1.Text = division
End If
I want to print 4 samples per page, and that is why I have an if statement for each mod value 0, 1, 2 and 3. But sometimes it works, othertimes not. For instance: 22 samples should print on 5 full pages, and 1 page with 2 on it, but according to the results I get for 22, it is 7 pages, not 6;
My thought was this: integer = integer / integer, will result in the integer rounded, so if the answer was actually 6.2, the integer part would be 6; likewise if the answer was 6.7, then my integer would be 7 because of rounding. Is this how it works? because sometimes it looks like with the mod 2 example I have to add 1 to get the correct number of pages, and other times I don't need too. Try it with 26 samples: 26 mod 4 is 2, and it tells me that I need 7 pages, which is correct with 6 pages of 4 samples, and 2 on the 7th page. This is confusing.
adam
You should turn Option Strict On immediately. You are using the standard division operator (/) and that returns a Double, which you then assign to an Integer variable. Bad. The fact that that is happening without your knowledge is why Option Strict Off, which is the default, is bad. In order to convert the Double to an Integer, the system rounds it. (22 / 4) is 5.5, which rounds up to 6 and then you're adding 1, giving 7. The complementary operation to Mod is actually \, which is integer division, not /. (22 \ 4) is 5.
Here's the sort of thing you should actually be doing:
Code:
Dim sampleCount As Integer
Dim samplesPerPage As Integer
Dim pageCount As Integer = CInt(Math.Ceiling(sampleCount / samplesPerPage))
Dim fullPageCount As Integer = sampleCount \ samplesPerPage
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
