Derived math functions Problem

G

Guest

Dear Users

In MS Access 97's derived math function of "Inverse Cosine" is
Arccos(X) = Atn(-X / Sqr(-X * X + 1)) + 2 * Atn(1)

However, it does not compute for input -1 and 1.
FYI: Input of -1 should yields 3.14 and input of 1 should yields 0.

Are there any workarounds?

Kind Regards
Charles
 
G

Graham Mandeno

Hi Charles

The problem is that 1 or -1 (or anything close) give a division by zero. I
think the best fix is to trap the error and return the required value:

Function ArcCos(X As Double) As Double
Const pi As Double = 3.14159263868863
On Error GoTo ProcErr
ArcCos = Atn(-X / Sqr(-X * X + 1)) + 2 * Atn(1)
ProcEnd:
Exit Function
ProcErr:
With Err
If .Number = 11 Then
' close to 1 or -1
If X > 0 Then
ArcCos = 0
Else
ArcCos = pi
End If
Else
Err.Raise .Number, .Source, .Description, .HelpFile, .HelpContext
End If
End With
Resume ProcEnd
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top