Announcement

Collapse
No announcement yet.

How to calculate ATN (Inverse Tangent)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to calculate ATN (Inverse Tangent)

    Hi,

    I would like to know how to calculate the ArcTangent or Inverse Tangent
    in code. So, not just using the great ATN function which is built in, in
    PowerBasic.

    Thanks in advance.

    Steven
    So here we are, this is the end.
    But all that dies, is born again.
    - From The Ashes (In This Moment)

  • #2
    [deleted]
    Last edited by Eddy Van Esch; 18 Mar 2008, 09:02 AM. Reason: (bad formatting)
    Eddy

    Comment


    • #3
      Code:
      You can use:
      
                                  x
          arctan(x) = arcsin (------------)
                              sqrt(1 + x^2)
      
      If x is large (larger than 1000-1500), then better use :
      
          arctan(x) = (PI / 2) - arctan(1 / |x|)
      
      and the sign of the result is the sign of original input.
      
      
      If 'x' is small enough use the following series:
      
                       x^3     x^5     x^7     x^9
       arctan(x) = x - ---  +  ---  -  ---  +  ---  ...
                        3       5       7       9
      
      It wil converge quickly if x is small enough.
      
      Kind regards
      Last edited by Eddy Van Esch; 18 Mar 2008, 09:04 AM.
      Eddy

      Comment


      • #4
        Steve,
        the fastest way is to use the ASM opcode for ATN but to do it yourself the easiest way is to follow the series shown here:
        http://thesaurus.maths.org/mmkb/entr...ryById&id=2649
        The more terms you use the more accurate answer you will get.
        Code:
        FUNCTION PBMAIN () AS LONG
        #REGISTER NONE
        
        LOCAL a,result AS EXT
        
        a = .5
        
        PRINT "ATN(a)=";ATN(a)
        
        !fld a           'get the value
        !fld1            'it calculate ATAN(a/b) to account for quadrants
        !fpatan          'do the calculation
        !fstp result     'store the result
        PRINT "ASM   =";result
        
        result = a - a^3/3 + a^5/5 - a^7/7 + a^9/9 - a^11/11 + a^13/13 - a^15/15  + a^17/17 'add more terms as needed
        PRINT "Series=";result
        
        WAITKEY$
        END FUNCTION
        Paul.

        Comment


        • #5
          Thanks to all for the assistance :-)
          So here we are, this is the end.
          But all that dies, is born again.
          - From The Ashes (In This Moment)

          Comment


          • #6
            My calculation for the last result:

            Code:
            #COMPILE EXE
            #DIM ALL
            FUNCTION PBMAIN () AS LONG
                #REGISTER NONE
             
                LOCAL i AS LONG
                LOCAL a,result,myinc,lastinc,mysign AS EXT
                a = .5##
                mysign=-1
                PRINT "ATN(a)=";ATN(a)
                !fld a           'get the value
                !fld1            'it calculate ATAN(a/b) to account for quadrants
                !fpatan          'do the calculation
                !fstp result     'store the result
                PRINT "ASM   =";result
                myinc=0.0##
                result=0.0##
                lastinc=0.0##
                FOR i = 1 TO 1000 STEP 2
                    'result = a - a^3/3 + a^5/5 - a^7/7 + a^9/9 - a^11/11 + a^13/13 - a^15/15  + a^17/17 'add more terms as needed
                    myinc =-mysign*a^i/i
                    IF ABS(lastinc)=ABS(myinc) THEN EXIT FOR
                    result=result+myinc
                    lastinc=myinc
                    mysign=-mysign
                NEXT i
             
                PRINT "Series=";result
                WAITKEY$
            END FUNCTION
            Regards,
            Bob

            Comment


            • #7
              Try the algorithm developed by Mr Henry Briggs (1561 - 1630) - no power, mul or div - only add and sub ... I need some time to do a search to find it here ...
              Last edited by Arthur Gomide; 25 Mar 2008, 07:24 AM.
              "The trouble with quotes on the Internet is that you can never know if they are genuine." - Abraham Lincoln.

              Comment

              Working...
              X