Anyone know of to get a Acos function with Cos??
The math object in vb only have cos, I need acos
Is there a way to get Acos(Something) = Cos(something)?????
/Smirre
Printable View
Anyone know of to get a Acos function with Cos??
The math object in vb only have cos, I need acos
Is there a way to get Acos(Something) = Cos(something)?????
/Smirre
It has ATan, you can get it using that and some other stuff I think.
Well, I know you can. I just forgot how :)
As far as I know, there is no Acos (inverse cosine) Function in VB, but there is an Atn (inverse tangent) Function. Remember that VB Trig Functions work with radians, not degrees. (Radians = Degrees * Pi / 180). Id est: 180 degrees = 3.14159 26535 89793 radians.
In most situations, the data is available to use the Atn Function. Think of a right triangle.If you have data for use with an inverse cosine/sine function, you usually have data for use with an inverse tangent function.
- Sine = OppositeSide / Hypotenuse ----- Y / R
- Cosine = AdjacentSide / Hypotenuse ----- X / R
- Tangent = OppositeSide / AdjacentSide ---- Y / X
In most of my work, I use my own inverse tangent function to avoid problems with angles close to or equal to 90 degrees.Note that zero / zero as well as Y / zero can cause a problem.Code:Public Function ArkTangent(Rise As Double, _
Run As Double) As Double
'Calling program is responsible for Quadrant'
'This function returns Radians in range 0 to Pie/2 (0 to 90 degrees)
Dim X As Double
Dim Y As Double
'Hpi = Pie/2 Qpi = Pie/4 (90 & 45 degrees)
Y = Abs(Rise)
X = Abs(Run)
If X > Y Then
ArkTangent = Atn(Y / X)
ElseIf X < Y Then
ArkTangent = Hpi - Atn(X / Y)
ElseIf X = 0 Then
ArkTangent = 0
Else
ArkTangent = Qpi '45 degrees
End If
End Function
If you use your own function instead of using Atn directly, you might want to include quadrant logic in the function.
I know its inconsequential, but 'pi' isn't spelt with an 'e' (at least I'm pretty sure it isn't).
Perhaps this is another example of Guv and his spelling rebellion? ;) Such an anarchist hehe
I forgot to include the following in my last post.
If you know the Cosine, use Atn as follows.
Tangent = Sqr( 1 - Cosine^2) / Cosine
Angle = Atn( Tangent )
Look up derived math functions in vb help file and voila!
Code:The following is a list of nonintrinsic math functions that can be derived from the intrinsic math functions:
Function Derived equivalents
Secant Sec(X) = 1 / Cos(X)
Cosecant Cosec(X) = 1 / Sin(X)
Cotangent Cotan(X) = 1 / Tan(X)
Inverse Sine Arcsin(X) = Atn(X / Sqr(-X * X + 1))
Inverse Cosine Arccos(X) = Atn(-X / Sqr(-X * X + 1)) + 2 * Atn(1)
Inverse Secant Arcsec(X) = Atn(X / Sqr(X * X – 1)) + Sgn((X) – 1) * (2 * Atn(1))
Inverse Cosecant Arccosec(X) = Atn(X / Sqr(X * X - 1)) + (Sgn(X) – 1) * (2 * Atn(1))
Inverse Cotangent Arccotan(X) = Atn(X) + 2 * Atn(1)
Hyperbolic Sine HSin(X) = (Exp(X) – Exp(-X)) / 2
Hyperbolic Cosine HCos(X) = (Exp(X) + Exp(-X)) / 2
Hyperbolic Tangent HTan(X) = (Exp(X) – Exp(-X)) / (Exp(X) + Exp(-X))
Hyperbolic Secant HSec(X) = 2 / (Exp(X) + Exp(-X))
Hyperbolic Cosecant HCosec(X) = 2 / (Exp(X) – Exp(-X))
Hyperbolic Cotangent HCotan(X) = (Exp(X) + Exp(-X)) / (Exp(X) – Exp(-X))
Inverse Hyperbolic Sine HArcsin(X) = Log(X + Sqr(X * X + 1))
Inverse Hyperbolic Cosine HArccos(X) = Log(X + Sqr(X * X – 1))
Inverse Hyperbolic Tangent HArctan(X) = Log((1 + X) / (1 – X)) / 2
Inverse Hyperbolic Secant HArcsec(X) = Log((Sqr(-X * X + 1) + 1) / X)
Inverse Hyperbolic Cosecant HArccosec(X) = Log((Sgn(X) * Sqr(X * X + 1) + 1) / X)
Inverse Hyperbolic Cotangent HArccotan(X) = Log((X + 1) / (X – 1)) / 2
Logarithm to base N LogN(X) = Log(X) / Log(N)
Kedaman: There is a typo in your post relating to derived mathematics formulae.Sine & cosine formulae have a symmetry which is lacking above.Code:Inverse Sine Arcsin(X) = Atn(X / Sqr(-X * X + 1))
Inverse Cosine Arccos(X) = Atn(-X / Sqr(-X * X + 1)) + 2 * Atn(1)
"+ 2 * Atn(1)" cannot be right.
At first, I thought you created the typo. Then I checked my VB Help File (I have MSDN Library), and it agreed with you. I still do not think it can be correct. Note the following (Verify with most any math text).
Tangent = Sine / Cosine
Sine = Sqr(1 - Cosine^2)
Cosine = Sqr(1 - Sine^2)
Tangent = Sqr(1 - Cosine^2) / Cosine
Tangent = Sine / Sqr(1 - Sine^2)
Angle = Atn( Tangent )
The above is consistent with the Derived Math formula for ArcSine , but inconsistent with the derived math for ArcCosine.
Do you agree?
Also: Why "-X * X + 1" instead of "1 - X * X"? The two froms are equivalent, but I have never seen the former in any other context.
Hi!
Thanks for the help everyone. I have solved the problem now, with your help!!
Thanks again!
/Smirre
Hi!
I have used this formula to get Acos.
Assume that X is the number to calc with.
Atn ( Sqr ( 1 - X * X ) / X )
In Excel I get this values:
Acos ( 0.2 ) = 1.369
Acos ( -0.2 ) = 1.772
This is correct. This is what I want.
However this is what I get from VB:
Atn ( Sqr ( 1 - 0.2 * 0.2 ) / 0.2) = 1.369
Atn ( Sqr ( 1 - 0.2 * -0.2 ) / -0.2) = -1.369
Is there a way to solve this??
Many thanks in andvance
/Smirre
Is there perhaps a dll or a component that I can download to get the ACOS function?
Smirre: I made an error in my analysis of this problem because I failed to think about the implication of negative cosines and the inverse cosine of negative values.
arcsin(-X) = -arcsin(X)
arctan(-X) = -arctan(X)
arccos(-X) = arccos(180 - X) In degrees.
The arccos formula posted by Kedaman is correct. It looks strange. The formula actually calculates arcsin(-X) and adds Pi/2 radians (90 degrees). This corrects for the cosine's different symmetry with respect to zero.
As far as I know, there is no inverse cosine function in VB. So far, I have always been able to do everything I need to do using Sin, Cos, Tan, & Atn functions in VB, without requiring the derived trig functions.
In real world problems, you can almost always use arctan instead of arccos or arcsin, provided that you understand the geometry/math relating to the trig functions. In the academic world, you are more often required to use arccos and arcsin.
Why do you want to calculate the arccos? Arccos of negative values results in second quandrant angles (90 to 180 degrees). Does your application require angles in the second quadrant, but not the third and fourth? This would be strange, if true.
Oh yes Guv - Happy Birthday :)
(sorry it's a bit late)
I'm designing a program to calculate port time duration on a two stroke engine.
When the duration is more than 180 degrees, the value to calculate acos with is less than zero. A port duration can be up to about 210 degrees.
So what formula do you suggest I use to fix this problem??
/Smirre
So far, I have always managed to use Atn Function in VB applications. I always have either have a point (X, Y) which is distance R from the origin (requiring quadrant logic) or a right triangle (No quadrant logic, angles are between 0 and 90 degrees). This leads to the use of the following formulae.
- Sine = Y / R or Y = R * Sine
- Cosine = X / R or X = R * cosine
- Tangent = Y / X or Y = X * Tangent
- Sine = OppositeSide / Hypotenuse
- Cosine = AdjacentSide / Hypotenuse
- Tangent = OppositeSide / AdjacentSide or Rise / Run
I always make my own Inverse Tangent Function which uses Atn internally. This avoids problems with division by zero, which can happen for the tangent. I usually put the quadrant logic in the mainline code because sometimes I want the angle in the range 0 to 360 degrees, and sometimes in the range minus 90 to plus 90 degrees. I usually declare constants for Pi, Pi/2, et cetera in the mainline. After copying the following from one of my applications, I added the quadrant logic (hope there is no typo here).Sometimes, I make my own Sine & Cosine Functions. I do this when there is some possibility of the origin being involved in trig computations for (X, Y) points. I take care of the problems with the origin in the Function. This is never necessary in engineering applications where you always seem to have a legitimate right triangle.Code:. . . .
Radians = ArkTangent( VerticalDistance, HorizontalDistance )
Degrees = Radians * 180 / Pie
. . . .
Public Function ArkTangent(Rise As Double, _
Run As Double) As Double
Dim X As Double
Dim Y As Double
Dim Angle As Double
'Pie = 3.14159 26535 89793 (180 degrees)
'Hpi = Pie/2 Qpi = Pie/4 (90 & 45 degrees)
Y = Abs(Rise)
X = Abs(Run)
If X > Y Then
Angle = Atn(Y / X)
ElseIf X < Y Then
Angle = Hpi - Atn(X / Y)
ElseIf X = 0 Then
Angle = 0
Else
Angle = Qpi '45 degrees
End If
If Run < 0 Then
If Rise < 0 Then '-- 3rd Quadrant'
ArkTangent = Pie + Angle
Else '-+ 2nd Quadrant'
ArkTangent = Pie - Angle
End If
ElseIf Rise < 0 Then '+- 4th Quadrant'
ArkTangent = Dpi - Angle
Else '++ 1st Quadrant'
ArkTangent = Angle
End If
End Function