 |
 |

06-07-2002, 04:24 PM
|
|
|
Division by Zero
|
How does VB handle dividion by zero. I am using it to trap invalid data and I am wondering if division by zero is going to slow the perfomance. Here is just an example.
Thanks,
D
Private Sub cmdUpdate_Click()
On Error GoTo divZero
Dim i As Integer
Dim j As Integer
Dim projectLine As String
If (lstforms.ListCount / lstProjects.ListCount) > 0 Then
For j = 1 To lstProjects.ListCount
For i = 1 To lstforms.ListCount
lstforms.ListIndex = i - 1
projectLine = BuildProjectLine(lstforms.Text)
Debug.Print projectLine
Next i
Next j
End If
Exit Sub
divZero:
MsgBox "Please enter all data correctly.", vbInformation, "Learn How To Work This"
End Sub
|
|

06-07-2002, 04:33 PM
|
 |
Lost Soul
Super Moderator * Guru *
|
|
Join Date: May 2001
Location: Vorlon
Posts: 18,931
|
|
I would do it like this:
Code:
Private Sub cmdUpdate_Click()
Dim i As Integer
Dim j As Integer
Dim projectLine As String
If lstProjects.ListCount = 0 Then
MsgBox "Please enter all data correctly.", vbInformation, "Learn How To Work This"
Exit Sub
End if
For j = 0 To lstProjects.ListCount - 1
For i = 0 To lstforms.ListCount - 1
projectLine = BuildProjectLine(lstforms.List(i))
Debug.Print projectLine
Next i
Next j
End Sub
|
|

06-07-2002, 04:35 PM
|
 |
Code Meister
Retired Moderator * Guru *
|
|
Join Date: Aug 2000
Location: Vancouver, BC, Canada
Posts: 10,441
|
|
|
It should not slow performance because you are only using it once, at the top of your loop. Error handling *is* a bit slow, but in your case it should not matter.
A larger problem is that your error handler will catch *any* unhandled error, not just div by zero, including those in your function BuildProjectLine...which can lead to unexpected results.
I would just have a simple
If lstProjects.ListCount Then
at the beginning to pass only a non-zero listcount. It's simple and more readable.
|
__________________
"I have a plan so cunning you could put a tail on it and call it a weasel!" - Edmund Blackadder
|

06-07-2002, 04:43 PM
|
|
|
|
Thanks Arn. I understand how to do this but what I was really looking for are the ramifications of using division by zero to trap errors/invalid data. Does it hurt/better the performance of a program.
Thanks again for the response.
D
|
|

06-07-2002, 04:44 PM
|
|
|
|
Too late on that one. Thanks Bill that is what I was looking for.
D
|
|

06-07-2002, 04:47 PM
|
 |
Code Meister
Retired Moderator * Guru *
|
|
Join Date: Aug 2000
Location: Vancouver, BC, Canada
Posts: 10,441
|
|
|
As I said, an error trap will trap ALL errors in scope, not just div by zero. So you then have to sort them out.
It's as if all errors were tossed into a big pile and your error handler then has to sort through them and figure out which came where.
For instance, suppose you had 2 places where a div by zero error could occur. How would you know which one actually triggered the error? How could you then fix it?
|
__________________
"I have a plan so cunning you could put a tail on it and call it a weasel!" - Edmund Blackadder
|

06-07-2002, 04:52 PM
|
 |
Lost Soul
Super Moderator * Guru *
|
|
Join Date: May 2001
Location: Vorlon
Posts: 18,931
|
|
|
Even worse your code contained multiple errors, which will raise an error in any given case.
I marked the correct code in bold. So don't say you know how to do this.
|
|

06-07-2002, 04:58 PM
|
|
|
|
I undertand all of this. The example I used was merely an attempt to better show what knowledge I was trying to obtain.
I was planning on using this for interpolating Taylor Polynomials. Eventually if I keep interpolating my error will come infinately close to zero. I wan't it to handle it "nicely" if I hit zero.
Thanks for the help though guys.
D
|
|

06-07-2002, 05:01 PM
|
|
|
|
Just because I don't use a list does not mean it is going to throw an error. So I don't know where you are getting multiple errors from.
|
|

06-07-2002, 05:01 PM
|
 |
Code Meister
Retired Moderator * Guru *
|
|
Join Date: Aug 2000
Location: Vancouver, BC, Canada
Posts: 10,441
|
|
|
The usual way to handle this situation is to have test for a value below a certain value. Otherwise, very small,yet non-zero values can trigger overflow errors when you try to divide by them....
|
__________________
"I have a plan so cunning you could put a tail on it and call it a weasel!" - Edmund Blackadder
|

06-07-2002, 05:07 PM
|
|
|
|
Never Mind Arn, I see what you mean now. I never ran this I just typed it in because I never had any vision that I would use it. Sorry I hit the fencepost on you.
Have a good weekend all.
D
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
|
|
 |
|