|
-
Apr 17th, 2001, 01:21 PM
#1
Thread Starter
Frenzied Member
Rattack: Following is some information relating to your problems with inverse cosine
using VB. I assume your problems are with the mathematics, not the code required to get data
from Text Boxes, format it, and put it into labels or other Text Boxes. For the latter type of
problems, I would post in the General Questions forum.
If possible, I would suggest using the Functions at the end of this post.
Visual Basic provides a minimum set of math functions. The documentation includes formulae
(See Derived Math Functions) for additional functions not intrinsic to VB. For the inverse
cosine, the following is suggested.
Arccos(X) = Atn(-X / Sqr(-X * X + 1)) + 2 * Atn(1)
The final term is a contrived way of expressing Pi/2 radians or 90 degrees. I would never do it
that way. In all my VB Math programs, I define constants for commonly used values like Pi,
2*Pi, Pi/2, e, et cetera.
The above formula returns a value between zero and Pi (0 and 180 degrees), which is often not a
suitable range. Note that the inverse tangent function has a problem at Pi/2 (90 degrees), where
1-X^2 in the above formula results in a zero divisor.
If you really want to compute the inverse cosine of a particular value using VB, the above is the
only way I know of doing it.
In almost all of the mathematical applications I have done, there is a context which suggests
using some other approach.
For engineering problems, you usually are dealing with a right triangle. The cosine is the ratio of
one side to the hypotenuse, while the tangent is the ratio of the two shorter sides. You can almost
always use the inverse tangent instead of inverse cosine. In the VB Functions provided below,
Engineers usually refer to Y and X as Rise and Run, or Opposite and Adjacent sides.
For most mathematical problems, you are usually working with a Cartesian (XY) coordinate
system, and want to deal with angles in ranges like 0 to 360, -90 to +90, -180 to +180. The sine
and cosine are Y/R & X/R, respectively (R is distance from origin), while the tangent is Y/X. It
is generally easier to work with inverse tangent of (Y/X) than inverse cosine of (X/R). If you
work with inverse cosine, you do not have enough data from (X/R) to know the quadrant of the
angle.
In all my programs, I use my own Longitude Function which returns an angle between 0 and
2*Pi (0 to 360 degrees), and a Latitude Function which returns an angle between -Pi/2 and +Pi/2
(-90 to +90 degrees). Both Functions use two arguments: Y & X, which can be positive or
negative. My functions take care of the problems at 90 degrees internally.
Code:
. . .
Public Const Dpi As Double = 6.28318530717959 '360 Degrees
Public Const Pie As Double = 3.14159265358979 '180
Public Const Hpi As Double = 1.5707963267949 ' 90
Public Const Qpi As Double = 0.785398163397448 ' 45
. . .
Public Function Longitude(Y As Double, X As Double) As Double
'Returns value in range 0 to Dpi (0 to 360 Degrees)
'Hpi = Pie/2 (90 Degrees) Qpi = Pie/4 (45 Degrees)
Dim Angle As Double
If Abs(X) > Abs(Y) Then 'must avoid divide by zero
Angle = Abs(Atn(Y / X))
ElseIf Abs(X) < Abs(Y) Then
Angle = Hpi - Abs(Atn(X / Y))
ElseIf X = 0 Then
Longitude = 0
Exit Function
Else
Angle = Qpi
End If
If Y < 0 Then 'Third or 4th quadrant.
If X < 0 Then '- - Third quadrant.
Longitude = Angle + Pie
Else '- + Fourth quadrant
Longitude = Dpi - Angle
End If
ElseIf X < 0 Then '+ - Second quadrant.
Longitude = Pie - Angle
Else '+ + First quadrant.
Longitude = Angle
End If
End Function
Public Function Latitude(Y As Double, X As Double) As Double
'Returns value in range -Hpi to +Hpi (-90 to +90 Degrees).
'Hpi = Pie/2 (90 Degrees) Qpi = Pie/4 (45 Degrees).
'X assumed non negative and treated as such.
'Quadrant might be incorrect If X is negative.
Dim Angle As Double
If Abs(X) > Abs(Y) Then 'must avoid divide by zero
Angle = Abs(Atn(Y / X))
ElseIf Abs(X) < Abs(Y) Then
Angle = Hpi - Abs(Atn(X / Y))
ElseIf X = 0 Then
Latitude = 0
Exit Function
Else
Angle = Qpi
End If
If Y < 0 Then
Latitude = -Angle
Else
Latitude = Angle
End If
End Function
I hope there are no typo's above, and apologize if there are. The VB Functions were copied and pasted from programs which worked for me. The odds are good that they are correct.
Live long & prosper.
The Dinosaur from prehistoric era prior to computers.
Eschew obfuscation!
If a billion people believe a foolish idea, it is still a foolish idea!
VB.net 2010 Express
64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.
-
Apr 18th, 2001, 04:52 AM
#2
Lively Member
-
Apr 25th, 2001, 02:07 AM
#3
Member
There is another way
The usual way such things as trigonometric functions are implemented is to use Taylor expansion series. check:
http://www.efunda.com/math/taylor_se...verse_trig.cfm
I used it for arcsin, however in this case it needed 200 chain members to be really precise. It could come in handy to program it in C++!
here's the code
Public Function ArcSin(x As Double) As Double
Dim n As Integer
Dim Last As Double
Dim Factor As Double
Last = 1
ArcSin = x
For n = 1 To 200 ' increase last n to enhance precision, n=200 give 3 correct digits after . for x=.99
Factor = ((2 * n) - 1) / (2 * n)
ArcSin = ArcSin + Last * Factor * ((x ^ ((2 * n) + 1)) / ((2 * n) + 1))
Last = Last * Factor
Next n
End Function
for arccos you just have to use:
arccos(x)=pi/2 - arcsin(x)
-
Apr 26th, 2001, 02:44 AM
#4
Lively Member
trig
fbokker
thankyou for your input.I will keep this in mind.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|