|
-
Aug 3rd, 2000, 09:31 AM
#1
Thread Starter
Lively Member
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
-
Aug 3rd, 2000, 09:39 AM
#2
Fanatic Member
if instr
if numbertobedivideby Mod dividedby = 0 then it is divisible
Kurt Simons
[I know I'm a hack but my clients don't!]
-
Aug 3rd, 2000, 09:39 AM
#3
Fanatic Member
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!]
-
Aug 3rd, 2000, 09:45 AM
#4
kurtsimons: That doesn't do it because for example 6.1 Mod 3 also returns 0 since the Mod function rounds to the nearest integer.
-
Aug 3rd, 2000, 09:51 AM
#5
Thread Starter
Lively Member
I'm only going to pass it Long Integers. Will it work in all cases then?
-Jack Vinitsky
-
Aug 3rd, 2000, 09:57 AM
#6
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
-
Aug 3rd, 2000, 09:59 AM
#7
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")
-
Aug 3rd, 2000, 10:03 AM
#8
Member
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.
-
Aug 3rd, 2000, 10:07 AM
#9
Hyperactive Member
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]
-
Aug 3rd, 2000, 10:22 AM
#10
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.
-
Aug 3rd, 2000, 10:26 AM
#11
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?
-
Aug 3rd, 2000, 10:32 AM
#12
Member
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?
-
Aug 3rd, 2000, 10:34 AM
#13
Hyperactive Member
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]
-
Aug 3rd, 2000, 10:35 AM
#14
Fanatic Member
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!]
-
Aug 3rd, 2000, 10:42 AM
#15
Hyperactive Member
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 )
-
Aug 3rd, 2000, 10:46 AM
#16
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.
-
Aug 3rd, 2000, 10:54 AM
#17
Member
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.
-
Aug 3rd, 2000, 10:59 AM
#18
Thread Starter
Lively Member
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
-
Aug 3rd, 2000, 05:02 PM
#19
transcendental analytic
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|