Results 1 to 17 of 17

Thread: mod math question

Hybrid View

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2013
    Posts
    242

    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????

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: mod math question

    41/4 = 10.25 Mod 4 = 2.25
    As the 6-dimensional 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!

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2013
    Posts
    242

    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

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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 6-dimensional 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!

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    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

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jan 2013
    Posts
    242

    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.

  8. #8
    Hyperactive Member
    Join Date
    May 2009
    Posts
    274

    Re: mod math question

    Quote Originally Posted by Adam Stallcup View Post
    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

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    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

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jan 2013
    Posts
    242

    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

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: mod math question

    Guess I didn't need to add the edit then...
    My usual boring signature: Nothing

  12. #12
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: mod math question

    my mathematics degree
    How truly it is said of mathematicians that their only problem is that they can't do arithmetic!
    As the 6-dimensional 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!

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Jan 2013
    Posts
    242

    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

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: mod math question

    Quote Originally Posted by Adam Stallcup View Post
    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

  15. #15
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  16. #16
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  17. #17
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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 6-dimensional 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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width