Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Acos function for VB 6?

Status
Not open for further replies.

Tim8w

Programmer
Apr 8, 2003
44
US
I need an Acos() and Asin() function for VB 6 similar to the built-in Atn() function. Why it was not included in VB, I'll never know.

Thanks,
Tim
 
Neither function is intrincis to VB, but you can use the following identities to derive these functions:
Code:
Arcsin(X) = Atn(X / Sqr(-X * X + 1))

Arccos(X) = Atn(-X / Sqr(-X * X + 1)) + 2 * Atn(1)
Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
I guess they wanted to see if you'd paid enough attention in math class to
know the identities. :)

(Based on trig section from Gary Connell's "VB6 from the Ground Up")

Code:
Public Function Pi() As Double
  Pi = 4 * Atn(1#)
End Function

Public Function ArcSin(ByVal x As Double) As Double
  ArcSin = Atn(x / Sqr(-x * x + 1))
End Function

Public Function ArcCos(ByVal x As Double) As Double
  ArcCos = ArcSin(x) + Pi / 2
End Function

x is in radians. It's left to you to do range-checking for x.
 
Thanks for the input guys. I'm still having a problem, however. ArcCos in both your implementations works fine. I have not been able to get a correct value for ArcSin using either impplementation...

Any ideas?

Thanks,
Tim
 
That's odd: in the functions I supplied ArcCos depends on ArcSin, so if ArcSin doesn't work, how would ArcCos?

Upon further examination, I see that CajunCenturion's ArcCos equation has a leading minus sign in the argument to Atn which I don't: both can't be right.

Ok, who still has their CRC Math Handbook?

Of course, you could always Google for "trig identities"...
 
I took your advice. I seached the web for ArcCos and found this code at:


Public Function ArcCos (x As Variant) As Variant

Select Case x
Case -1
ArcCos = 4 * Atn(1)

Case 0:
ArcCos = 2 * Atn(1)

Case 1:
ArcCos = 0

Case Else:
ArcCos = Atn(-x / Sqr(-x * x + 1)) + 2 * Atn(1)
End Select
End Function

Public Function ArcSin (x As Variant) As Variant

Select Case x
Case -1
ArcSin = 6 * Atn(1)

Case 0:
ArcSin = 0

Case 1:
ArcSin = 2 * Atn(1)

Case Else:
ArcSin = Atn(x / Sqr(-x * x + 1))
End Select
End Function

Seems all I was missing was the extreeme cases.

Thanks all,
Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top