Division by Zero error

kliq316

New member
Local time
Yesterday, 19:40
Joined
May 28, 2009
Messages
1
Hi Guys,
I'm hoping you can help me here ... I know a bit about Access, but not enough to be efficient yet!! I have a module with the following function to calculate arcCOS value. The only trouble is, on certain records used in the query, it pulls up a "Division by zero" error on the code. Is there any way I can prevent this from happening? Here's the code I'm using:

Public Function ArcCOS(ByVal nValue As Double, Optional fRadians As Boolean = True) As Double
Const PI As Double = 3.14159265359
ArcCOS = -Atn(nValue / Sqr(1 - nValue * nValue)) + PI / 2
If fRadians = False Then ArcCOS = ArcCOS * (PI / 180)
End Function
 
The only place I can see it come up with that error is here:
/ Sqr(1 - nValue * nValue))

The solution would be to do 'something' to stop this part of the function beeing 0.
I.e.
If Sqr(1 - nValue * nValue)) = 0 then exit function

Good luck!
 
This will only occur if nValue has a value of 1 so add:

Code:
if nValue <> 1 and_
   nvalue <> -1 then
Const PI As Double = 3.14159265359
ArcCOS = -Atn(nValue / Sqr(1 - nValue * nValue)) + PI / 2
If fRadians = False Then ArcCOS = ArcCOS * (PI / 180)
end if
 
Last edited:
Just put in a test for zero before you do the division. try something like
Code:
Public Function ArcCOS(ByVal nValue As Double, Optional fRadians As Boolean = True) As Double
Const PI As Double = 3.14159265359
if Sqr(1 - nValue * nValue) = zero then
  put in what you want to do if divisor = 0 
else
  ArcCOS = -Atn(nValue / Sqr(1 - nValue * nValue)) + PI / 2
  If fRadians = False then
     ArcCOS = ArcCOS * (PI / 180)
 end if
end if
End Function
 
LOL LOL LOL LOL LOL LOL LOL LOL LOL LOL LOL LOL LOL LOL

3 times the same advice in 3 minutes :) Must be something right about that :D
 

Users who are viewing this thread

Back
Top Bottom