Wrox Home  
Search P2P Archive for: Go

  Return to Index  

access thread: division by zero


Message #1 by "NAME REMOVED (Ext. 816)" <EMAIL REMOVED> on Thu, 19 Sep 2002 13:40:00 +0100
Hi,
can anyone tell me how we handle division by zero, e.g.. 10/0 = #Num!.

Thanks 
NAME REMOVED


**************************************************************************
The information contained in this e-mail is confidential,
may be privileged and is intended only for the use of the
recipient named above. If you are not the intended
recipient or a representative of the intended recipient,
you have received this e-mail in error and must not copy,
use or disclose the contents of this email to anybody
else. If you have received this e-mail in error, please
notify the sender immediately by return e-mail and
permanently delete the copy you received. This email has
been swept for computer viruses. However, you should
carry out your own virus checks.


Registered in Ireland, No. 205721. http://www.FINEOS.com
**************************************************************************

Message #2 by "Gerald, Rand" <RGerald@u...> on Thu, 19 Sep 2002 10:13:05 -0500
I like to use the iif() function. For example: if(denominator =3D 0, 0,
numerator / denominator).


Rand E Gerald
Database Specialist
Information Services / Operations
Bah=E1'=ED National Office
1233 Central St.
Evanston IL 60201
(xxx) xxx-xxxx

-----Original Message-----
From: NAME REMOVED (Ext. 816) [mailto:EMAIL REMOVED]
Sent: Thursday, September 19, 2002 7:40 AM
To: Access
Subject: [access] division by zero

Hi,
can anyone tell me how we handle division by zero, e.g.. 10/0 =3D 
#Num!.

Thanks
NAME REMOVED


************************************************************************
**
The information contained in this e-mail is confidential,
may be privileged and is intended only for the use of the
recipient named above. If you are not the intended
recipient or a representative of the intended recipient,
you have received this e-mail in error and must not copy,
use or disclose the contents of this email to anybody
else. If you have received this e-mail in error, please
notify the sender immediately by return e-mail and
permanently delete the copy you received. This email has
been swept for computer viruses. However, you should
carry out your own virus checks.


Registered in Ireland, No. 205721. http://www.FINEOS.com
************************************************************************
**


Message #3 by "Gerald, Rand" <RGerald@u...> on Thu, 19 Sep 2002 10:15:43 -0500
OOPS: The line should be:

iif(denominator =3D 0, 0, numerator / denominator).


Rand E Gerald
Database Specialist
Information Services / Operations
Bah=E1'=ED National Office
1233 Central St.
Evanston IL 60201
(xxx) xxx-xxxx

-----Original Message-----
From: Gerald, Rand [mailto:RGerald@u...]
Sent: Thursday, September 19, 2002 10:13 AM
To: Access
Subject: [access] RE: division by zero
Importance: High

I like to use the iif() function. For example: if(denominator =3D 0, 0,
numerator / denominator).


Rand E Gerald
Database Specialist
Information Services / Operations
Bah=E1'=ED National Office
1233 Central St.
Evanston IL 60201
(xxx) xxx-xxxx

-----Original Message-----
From: NAME REMOVED (Ext. 816) [mailto:EMAIL REMOVED]
Sent: Thursday, September 19, 2002 7:40 AM
To: Access
Subject: [access] division by zero

Hi,
can anyone tell me how we handle division by zero, e.g.. 10/0 =3D 
#Num!.

Thanks
NAME REMOVED


************************************************************************
**
The information contained in this e-mail is confidential,
may be privileged and is intended only for the use of the
recipient named above. If you are not the intended
recipient or a representative of the intended recipient,
you have received this e-mail in error and must not copy,
use or disclose the contents of this email to anybody
else. If you have received this e-mail in error, please
notify the sender immediately by return e-mail and
permanently delete the copy you received. This email has
been swept for computer viruses. However, you should
carry out your own virus checks.


Registered in Ireland, No. 205721. http://www.FINEOS.com
************************************************************************
**



Message #4 by "Bob Bedell" <bobbedell15@m...> on Thu, 19 Sep 2002 19:56:44 +0000
Unfortunately VBA has this funny quirk: if denominator is actually
0, you'll get the runtime error using IIf. VBA evaluates both portions
of the IIf statment. When it evaluates part three,
numerator / denominator, the divide by 0 error occurs. To see this,
paste the following sub into a module:

Sub Test(intY As Integer, intX As Integer)
    Dim dblNew As Double

    dblNew = IIf(intY = 0, 0, intX / intY)

End Sub

Now type Call Test(0,1) in the immediate window and hit Enter. Your
code crashes. 0 = 0, right? So IIf should return 0. Unfortunnately, it
goes on to evaluate condition two, divides intX by intY, and boom.

An If...Then structure is the safe way to go as in Function Divide
below:

Sub Test1(numerator As Integer, denominator As Integer)
    Dim result As String

    result = Divide(numerator, denominator)

    'Value of result is "" if exit function executed
    If result = "" Then
        MsgBox "Divide by zero attempted"
    Else
        MsgBox result
    End If
End Sub

Private Function Divide(n As Integer, d As Integer) As String

        If d = 0 Then
            Exit Function  'result = ""
        Else
            Divide = n / d
        End If

End Function

Respectfully,

Bob
>From: "Gerald, Rand" <RGerald@u...>
>Reply-To: "Access" <access@p...>
>To: "Access" <access@p...>
>Subject: [access] RE: division by zero
>Date: Thu, 19 Sep 2002 10:13:05 -0500
>
>I like to use the iif() function. For example: if(denominator = 0, 0,
>numerator / denominator).
>
>
>Rand E Gerald
>Database Specialist
>Information Services / Operations
>Bahá'í National Office
>1233 Central St.
>Evanston IL 60201
>(xxx) xxx-xxxx
>
>-----Original Message-----
>From: NAME REMOVED (Ext. 816) [mailto:EMAIL REMOVED]
>Sent: Thursday, September 19, 2002 7:40 AM
>To: Access
>Subject: [access] division by zero
>
>Hi,
>can anyone tell me how we handle division by zero, e.g.. 10/0 = #Num!.
>
>Thanks
>NAME REMOVED
>
>
>**************************************************************************
>The information contained in this e-mail is confidential,
>may be privileged and is intended only for the use of the
>recipient named above. If you are not the intended
>recipient or a representative of the intended recipient,
>you have received this e-mail in error and must not copy,
>use or disclose the contents of this email to anybody
>else. If you have received this e-mail in error, please
>notify the sender immediately by return e-mail and
>permanently delete the copy you received. This email has
>been swept for computer viruses. However, you should
>carry out your own virus checks.
>
>
>Registered in Ireland, No. 205721. http://www.FINEOS.com
>**************************************************************************
>
>
>




_________________________________________________________________
Join the world?s largest e-mail service with MSN Hotmail. 
http://www.hotmail.com

Message #5 by "Gerald, Rand" <RGerald@u...> on Thu, 19 Sep 2002 15:40:26 -0500
Ok, then try this or your own variant.

' =3D=3D=3D=3D=3D=3D=3D=3D begin code 
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

Public Function SafeDivide(numNum As Double, numDen As Double, Optional
intRtn As Integer) As Double
    Dim dblRtn As Double
    If IsMissing(intRtn) Then
        intRtn =3D 1
    End If
   
    If numDen =3D 0# Then
        Select Case intRtn
            Case 1          ' Return 0 if 0
                dblRtn =3D 0
            Case 2          ' Return 1 if 0
                dblRtn =3D 1
            Case 3          ' Return numerator if 0
                dblRtn =3D numNum
            Case Else
                dblRtn =3D 0
         End Select
    Else
        dblRtn =3D numNum / numDen
    End If
   
    SafeDivide =3D dblRtn
End Function

' =3D=3D=3D=3D=3D=3D=3D=3D end code 
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

Rand E Gerald
Database Specialist
Information Services / Operations
Bah=E1'=ED National Office
1233 Central St.
Evanston IL 60201
(xxx) xxx-xxxx

-----Original Message-----
From: Bob Bedell [mailto:bobbedell15@m...]
Sent: Thursday, September 19, 2002 2:57 PM
To: Access
Subject: [access] RE: division by zero

Unfortunately VBA has this funny quirk: if denominator is actually
0, you'll get the runtime error using IIf. VBA evaluates both portions
of the IIf statment. When it evaluates part three,
numerator / denominator, the divide by 0 error occurs. To see this,
paste the following sub into a module:

Sub Test(intY As Integer, intX As Integer)
    Dim dblNew As Double

    dblNew =3D IIf(intY =3D 0, 0, intX / intY)

End Sub

Now type Call Test(0,1) in the immediate window and hit Enter. Your
code crashes. 0 =3D 0, right? So IIf should return 0. Unfortunnately, 
it
goes on to evaluate condition two, divides intX by intY, and boom.

An If...Then structure is the safe way to go as in Function Divide
below:

Sub Test1(numerator As Integer, denominator As Integer)
    Dim result As String

    result =3D Divide(numerator, denominator)

    'Value of result is "" if exit function executed
    If result =3D "" Then
        MsgBox "Divide by zero attempted"
    Else
        MsgBox result
    End If
End Sub

Private Function Divide(n As Integer, d As Integer) As String

        If d =3D 0 Then
            Exit Function  'result =3D ""
        Else
            Divide =3D n / d
        End If

End Function

Respectfully,

Bob
>From: "Gerald, Rand" <RGerald@u...>
>Reply-To: "Access" <access@p...>
>To: "Access" <access@p...>
>Subject: [access] RE: division by zero
>Date: Thu, 19 Sep 2002 10:13:05 -0500
>
>I like to use the iif() function. For example: if(denominator =3D 0, 
0,
>numerator / denominator).
>
>
>Rand E Gerald
>Database Specialist
>Information Services / Operations
>Bah=E1'=ED National Office
>1233 Central St.
>Evanston IL 60201
>(xxx) xxx-xxxx
>
>-----Original Message-----
>From: NAME REMOVED (Ext. 816) [mailto:EMAIL REMOVED]
>Sent: Thursday, September 19, 2002 7:40 AM
>To: Access
>Subject: [access] division by zero
>
>Hi,
>can anyone tell me how we handle division by zero, e.g.. 10/0 =3D 
#Num!.
>
>Thanks
>NAME REMOVED
>
>
>***********************************************************************
***
>The information contained in this e-mail is confidential,
>may be privileged and is intended only for the use of the
>recipient named above. If you are not the intended
>recipient or a representative of the intended recipient,
>you have received this e-mail in error and must not copy,
>use or disclose the contents of this email to anybody
>else. If you have received this e-mail in error, please
>notify the sender immediately by return e-mail and
>permanently delete the copy you received. This email has
>been swept for computer viruses. However, you should
>carry out your own virus checks.
>
>
>Registered in Ireland, No. 205721. http://www.FINEOS.com
>***********************************************************************
***
>
>
>




_________________________________________________________________
Join the world's largest e-mail service with MSN Hotmail.
http://www.hotmail.com


Message #6 by "Gregory Serrano" <SerranoG@m...> on Fri, 20 Sep 2002 13:21:45
NAME REMOVED, 

<< can anyone tell me how we handle division by zero, e.g.. 10/0 = #Num!. 
>>

The simpliest way is to do and IF THEN ELSE statement.

   If Me.txtDenominator = 0 Then
      Me.txtResult = 0
      Msgbox "The denominator is zero.  Result set to zero.", _
         vbExclamation, "Divide by Zero Error!"
   Else
      Me.txtResult = Me.txtNumerator / Me.txtDenominator
   End If

Note, a message should usually accompany the result when the denominator 
is zero because simply setting the result to zero is an arbitrary 
assigment.  It's not a "true" answer.  In most scientific and engineering 
cases, a value of zero in the denominator usually signifies a specific 
physical property has reached a certain state.  In that case, the "laws of 
physics" or the implication means that Me.txtResult should be set to 
another default, not zero.  In either case, send the user a message.

Greg

  Return to Index