DevLabs > DevLabs Forums > Small Basic > Arc Tangent in Small Basic
Ask a questionAsk a question
 

AnswerArc Tangent in Small Basic

  • Monday, April 06, 2009 7:41 PMCoding CatAnswererUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I have an equation that is relatively simply using the ArcTan function (angle between two points) and nearly impossible without it. Without accessing an external library, can someone give me a SmallBasic equivalent for ArcTan?

Answers

  • Monday, April 06, 2009 8:32 PMVijaye RajiMSFT, OwnerUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Using a convergent series, this gives a "close-enough" answer

      Sub ArcTan
        If x > 1 then
          x = 1 / x
          ArcTanInternal()
          y = Math.Pi / 2 - y
        Else
          ArcTanInternal()
        EndIf
      EndSub

      Sub ArcTanInternal
        y = x - Math.Power(x, 3) / 3 + Math.Power(x, 5) / 5 - Math.Power(x, 7) / 7
      EndSub

    Sample program published as: QHL110

All Replies

  • Monday, April 06, 2009 8:28 PMlitdevAnswererUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Using radians

    For |x| < 1 : arctan(x) ~ x/(1+0.28*x^2)

    For |x| > 1 : arctan(x) ~ pi/2 - x/(x^2 + 0.28)
  • Monday, April 06, 2009 8:32 PMVijaye RajiMSFT, OwnerUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Using a convergent series, this gives a "close-enough" answer

      Sub ArcTan
        If x > 1 then
          x = 1 / x
          ArcTanInternal()
          y = Math.Pi / 2 - y
        Else
          ArcTanInternal()
        EndIf
      EndSub

      Sub ArcTanInternal
        y = x - Math.Power(x, 3) / 3 + Math.Power(x, 5) / 5 - Math.Power(x, 7) / 7
      EndSub

    Sample program published as: QHL110

  • Monday, April 06, 2009 8:33 PMVijaye RajiMSFT, OwnerUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    The resulting angle is in Radians, you can use Math.GetDegrees to convert the angle to degrees.
  • Monday, April 06, 2009 10:01 PMFremyCompanyAnswererUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Using a convergent series, this gives a "close-enough" answer
    I don't find that a "close-enough" answer :)

    It's only a rough approximation... This is the best thing we can do for the moment but it's not really a pretty way to do the thing.

    Fremy - Developer in VB.NET, C# and JScript ... - Feel free to try my extension
  • Tuesday, April 07, 2009 1:36 AMCoding CatAnswererUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thank you.

    And add the Arc-Trig functions to the wish list.
  • Tuesday, April 07, 2009 2:30 PMLoyola IT Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    That convergent series can be run out there a long ways to get an even closer "close-enough" value.
  • Wednesday, April 08, 2009 7:35 PMLoyola IT Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Here is a simple way if getting that arctan value.  I just ran that Taylor series out to 99.  It is the same as my TI-89 to at least 6 decimal places.  We got men on the moon with less accuracy.
    x = 1.5
    ArcTan()
    TextWindow.WriteLine(y)
    
    x = 0.6 
    ArcTan()
    TextWindow.WriteLine(y)
    
    Sub ArcTan
      If x > 1 then
        x = 1 / x
        ArcTanInternal()
        y = Math.Pi / 2 - y
      Else
        ArcTanInternal()
      EndIf
    EndSub
    
    Sub ArcTanInternal
      'y = x - Math.Power(x, 3) / 3 + Math.Power(x, 5) / 5 - Math.Power(x, 7) / 7
      sign = -1
      y = 0
      For p = 1 To 99 Step 2
        sign = - sign
        y = y + sign * Math.Power(x, p)/p
      EndFor
    EndSub