Results 1 to 14 of 14

Thread: Acos and Cos problem???

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Location
    Kalix, Norrbotten, SWEDEN
    Posts
    343
    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
    Visual Basic
    C, C++
    Java
    Access
    SQL Server

    MCP, MCSD

  2. #2
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    It has ATan, you can get it using that and some other stuff I think.
    Harry.

    "From one thing, know ten thousand things."

  3. #3
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Well, I know you can. I just forgot how
    Harry.

    "From one thing, know ten thousand things."

  4. #4
    Frenzied Member
    Join Date
    Jul 1999
    Location
    Huntingdon Valley, PA 19006
    Posts
    1,151

    Use Atn Function.

    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.
    • Sine = OppositeSide / Hypotenuse ----- Y / R
    • Cosine = AdjacentSide / Hypotenuse ----- X / R
    • Tangent = OppositeSide / AdjacentSide ---- Y / X
    If you have data for use with an inverse cosine/sine function, you usually have data for use with an inverse tangent function.

    In most of my work, I use my own inverse tangent function to avoid problems with angles close to or equal to 90 degrees.
    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
    Note that zero / zero as well as Y / zero can cause a problem.

    If you use your own function instead of using Atn directly, you might want to include quadrant logic in the function.
    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.

  5. #5
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    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
    Harry.

    "From one thing, know ten thousand things."

  6. #6
    Frenzied Member
    Join Date
    Jul 1999
    Location
    Huntingdon Valley, PA 19006
    Posts
    1,151

    BTW.

    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 )
    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.

  7. #7
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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)
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  8. #8
    Frenzied Member
    Join Date
    Jul 1999
    Location
    Huntingdon Valley, PA 19006
    Posts
    1,151

    Typo.

    Kedaman: There is a typo in your post relating to derived mathematics formulae.
    Code:
    Inverse Sine	Arcsin(X) = Atn(X / Sqr(-X * X + 1))
    Inverse Cosine	Arccos(X) = Atn(-X / Sqr(-X * X + 1)) + 2 * Atn(1)
    Sine & cosine formulae have a symmetry which is lacking above.

    "+ 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.
    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.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Location
    Kalix, Norrbotten, SWEDEN
    Posts
    343

    Thanks!!

    Hi!

    Thanks for the help everyone. I have solved the problem now, with your help!!

    Thanks again!

    /Smirre
    Visual Basic
    C, C++
    Java
    Access
    SQL Server

    MCP, MCSD

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Location
    Kalix, Norrbotten, SWEDEN
    Posts
    343

    Question sorry, tought it worked, but no...

    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?
    Last edited by Smirre; Feb 10th, 2001 at 06:32 AM.
    Visual Basic
    C, C++
    Java
    Access
    SQL Server

    MCP, MCSD

  11. #11
    Frenzied Member
    Join Date
    Jul 1999
    Location
    Huntingdon Valley, PA 19006
    Posts
    1,151

    My error.

    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.
    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.

  12. #12
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Oh yes Guv - Happy Birthday

    (sorry it's a bit late)
    Harry.

    "From one thing, know ten thousand things."

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Location
    Kalix, Norrbotten, SWEDEN
    Posts
    343

    Arccos

    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
    Visual Basic
    C, C++
    Java
    Access
    SQL Server

    MCP, MCSD

  14. #14
    Frenzied Member
    Join Date
    Jul 1999
    Location
    Huntingdon Valley, PA 19006
    Posts
    1,151

    Some thots & code.

    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).
    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
    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.
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width