Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > Division by Zero


Reply
 
Thread Tools Display Modes
  #1  
Old 06-07-2002, 04:24 PM
d_hadzima
Guest
 
Posts: n/a
Default 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
Reply With Quote
  #2  
Old 06-07-2002, 04:33 PM
Flyguy's Avatar
Flyguy Flyguy is offline
Lost Soul

Super Moderator
* Guru *
 
Join Date: May 2001
Location: Vorlon
Posts: 18,931
Default

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
Reply With Quote
  #3  
Old 06-07-2002, 04:35 PM
BillSoo's Avatar
BillSoo BillSoo is offline
Code Meister

Retired Moderator
* Guru *
 
Join Date: Aug 2000
Location: Vancouver, BC, Canada
Posts: 10,441
Default

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
Reply With Quote
  #4  
Old 06-07-2002, 04:43 PM
d_hadzima
Guest
 
Posts: n/a
Default

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
Reply With Quote
  #5  
Old 06-07-2002, 04:44 PM
d_hadzima
Guest
 
Posts: n/a
Default

Too late on that one. Thanks Bill that is what I was looking for.
D
Reply With Quote
  #6  
Old 06-07-2002, 04:47 PM
BillSoo's Avatar
BillSoo BillSoo is offline
Code Meister

Retired Moderator
* Guru *
 
Join Date: Aug 2000
Location: Vancouver, BC, Canada
Posts: 10,441
Default

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
Reply With Quote
  #7  
Old 06-07-2002, 04:52 PM
Flyguy's Avatar
Flyguy Flyguy is offline
Lost Soul

Super Moderator
* Guru *
 
Join Date: May 2001
Location: Vorlon
Posts: 18,931
Default

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.
Reply With Quote
  #8  
Old 06-07-2002, 04:58 PM
d_hadzima
Guest
 
Posts: n/a
Default

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
Reply With Quote
  #9  
Old 06-07-2002, 05:01 PM
d_hadzima
Guest
 
Posts: n/a
Default

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.
Reply With Quote
  #10  
Old 06-07-2002, 05:01 PM
BillSoo's Avatar
BillSoo BillSoo is offline
Code Meister

Retired Moderator
* Guru *
 
Join Date: Aug 2000
Location: Vancouver, BC, Canada
Posts: 10,441
Default

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
Reply With Quote
  #11  
Old 06-07-2002, 05:07 PM
d_hadzima
Guest
 
Posts: n/a
Default

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
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->