Results 1 to 19 of 19

Thread: Division Function

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2000
    Posts
    80
    Hi, I need help writing a function that will return TRUE if a number passed to it is divisible by 3; else it returns FALSE. Thanks in advance.

    -Jack Vinitsky

  2. #2
    Fanatic Member
    Join Date
    Jul 2000
    Location
    Manchester NH
    Posts
    833

    if instr

    if numbertobedivideby Mod dividedby = 0 then it is divisible
    Kurt Simons
    [I know I'm a hack but my clients don't!]

  3. #3
    Fanatic Member
    Join Date
    Jul 2000
    Location
    Manchester NH
    Posts
    833

    if 6 mod 3 = 0 then
    ' it is
    else
    it is not
    end if
    Kurt Simons
    [I know I'm a hack but my clients don't!]

  4. #4

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jun 2000
    Posts
    80
    I'm only going to pass it Long Integers. Will it work in all cases then?

    -Jack Vinitsky

  6. #6
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Yes, but you might want to validate anyhow with something like this:
    Code:
    Public Function DivBy3(varValue As Variant) As Boolean
    
        ' See if any fractional numbers are passed
        If Int(varValue) <> varValue Then
            MsgBox "Invalid value"
            DivBy3 = False
            Exit Function
        End If
        
        If varValue Mod 3 = 0 Then
            DivBy3 = True
        Else
            DivBy3 = False
        End If
    
    End Function

  7. #7
    Guest
    Try this.

    Code:
    Function CheckNum(iNum, iDivideBy) As Boolean
    
        If iNum / iDivideBy = CInt(iNum / iDivideBy) Then
            CheckNum = True
        Else
            CheckNum = False
        End If
        
    End Function
    Usage
    Code:
    'Check if Text1 is disible by 3
    Retval = CheckNum(Text1.Text, 3)
    If Retval = True Then MsgBox ("It's divisible by 3")

  8. #8
    Member
    Join Date
    Jun 2000
    Posts
    37

    Long integers work fine

    If you're only going to pass it long integers it should work in every case in all situations. While I am hesitant to challenge a guru, I really don't see any point in passing a variant into the function then validate, when passing a long will work quicker and easier.

  9. #9
    Hyperactive Member
    Join Date
    May 2000
    Posts
    367
    Shub, I agree with you. When working on applications used by 5,000 users, one thing you find out they like is speed, and frankly variants are not the answer, use a long with substantial error checking.


    although if it is for a program that is not real important, variants work good too.

    [Edited by billrogers on 08-03-2000 at 11:15 AM]

  10. #10
    Guest
    Are you referring to me or MartinLiss? If you're referring to me, I passed it as a variant because Integer's and Long Integer's cannot be fractional therefor we can be more flexible by allowing Singles and Doubles to be passed as well.

  11. #11
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    shub: While JackV said he "only going to pass it Long Integers", does it really hurt to check? What if someday some other person maintained the program and allowed something else to be passed to the function? It might be some time befor someone realized that bad results were being returned. I strongly believe that it's good programming practice to "trust your friends but check anyhow".

    billrogers: Do you really believe that the difference between handling a variant or a long would be noticible by the user (unless of course thousands of calculations in a row were done that way)? And also are you sure that your "long with substantial error checking" would faster than using a Variant?

  12. #12
    Member
    Join Date
    Jun 2000
    Posts
    37
    Well if the function's argument was a long, vb would give a compile error if someone tried to pass anything besides a long. Therefore there is no assumption involved. However if the argument was made a varient, that leaves others a lot more room to use it improperly.

    As for the speed issue, true it doesn't matter unless there are many calculations in a row. But who's to say nobody will call that function in a loop? When you have a choice to make something faster or slower, why make it slower?

  13. #13
    Hyperactive Member
    Join Date
    May 2000
    Posts
    367
    marty, you know as you must, being a guru, that variants are slower than heck. no one was trying to challenge you on being a guru, but why use a variant when you dont have to?

    What I meant by error checking, is make sure it doesnt bomb out, i.e. letting something be divided by 0. A crash is way worse then a perforamance issue of having error checking, or even of a variant use.


    [Edited by billrogers on 08-03-2000 at 11:38 AM]

  14. #14
    Fanatic Member
    Join Date
    Jul 2000
    Location
    Manchester NH
    Posts
    833
    this will work for anything
    I was thinking only of intergers before
    --------------------------------------

    Private Sub Command1_Click()
    If IsDivisible(6, 1.5) = True Then
    Beep
    End If

    End Sub

    Public Function IsDivisible(num As String, div As String) As Boolean
    Dim a As String

    a = num / div
    If InStr(a, ".") = 0 Then IsDivisible = True


    End Function
    Kurt Simons
    [I know I'm a hack but my clients don't!]

  15. #15
    Hyperactive Member
    Join Date
    May 2000
    Posts
    367
    questions of your man/woman hood, or skill in programming are not intended, if anyone has taken offense, I offer my aplogies, but as any programmer knows, one can learn the most from peer reviews and discussion such as this.

    Again, my aplogies, (sorry about the spelling not sure how the heck to spell that word )

  16. #16
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    shub: You are correct if the value passed has an explicit type, so for example the following would not compile.
    Code:
    Dim MyVar as Double
    MyVar = 123.45
    
    DivBy3 MyVar
    But if 123.45 were in Text1, then DivBy3 Val(Text1.Text) would not cause a compile error. VB would simply truncate the fractional portion.

  17. #17
    Member
    Join Date
    Jun 2000
    Posts
    37
    That makes sense. But then it comes down to where you want to check for this error. It believe that a function should just have one purpose, this makes the code a lot clearer. So a function to find if a long is divisible by 3 should only check if a long is divisible by 3. If you want to truncate values that can be done before calling the function.

  18. #18

    Thread Starter
    Lively Member
    Join Date
    Jun 2000
    Posts
    80
    Wow, who would have thought that such an innocent question can have so many issues involved with it? You think some things are going to be easily solved.

    Thanks again for all your help folks.

    -Jack

  19. #19
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    LOL, this surely isn't as unusual as you think, i've seen worse. But i think Meg's function was best, except that you could improove it;
    Code:
    Function CheckNum(iNum, iDivideBy) As Boolean
        CheckNum = iNum / iDivideBy = Int(iNum / iDivideBy) 
    End Function
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

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