Divide by zero

Any ratio function with X on the bottom has a chance of the denominator being zero, which is a program ending event without error handling. When Y = K/X where X is approaching zero then Y is approaching infinity, either negatively or positively. Its a vertical asymptote. The best solution would be to just not draw that interior line that crosses the asymptote, in effect imagine arrowheads on the ends of the lines drawn pointing to infinity. The second best solution and a much easier one is to capture the error and set the value of Y to zero when the denominator is zero. With the imprecision of doubles variables this happens less than it should theoretically. Nevertheless we have to set an error trap for divide by zero and have a plan when it happens. Setting the value of Y to zero is a good compromise. The lines that are heading to infinity are stopped and the line that *approximates* the asymptote is drawn. For accuracy, the line should then be erased.


The equation functions return a single value for Y and are a natural place for the error handler. It’s only required on the functions with an X value in the denominator.

Function C_06(X As Double) As Double
On Error GoTo HandlError

C_06 = (A * (X ^ 2) + B * X + C) / (D * (X ^ 2) + E * X + F)

Exit Function

If Err.Number = 11 Then
MsgBox "div by zero"
C_06 = 0
End If

End Function

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s