Author Topic: Trigonometric functions (tan, arctan) in axe  (Read 2932 times)

0 Members and 1 Guest are viewing this topic.

Offline bilyp

  • LV2 Member (Next: 40)
  • **
  • Posts: 21
  • Rating: +9/-1
  • Axing the z80
    • View Profile
Trigonometric functions (tan, arctan) in axe
« on: November 15, 2011, 01:10:55 am »
How would you go about doing trigonometric functions in axe, like the arctan (tan-1()), to calculate the reverse of the tangent or sine of an angle.
I looked all over the internet, but all I could find was that tan(X) = sin(X)/cos(X), nothing about calculating the arctangent. :(

Offline Quigibo

  • The Executioner
  • CoT Emeritus
  • LV11 Super Veteran (Next: 3000)
  • *
  • Posts: 2031
  • Rating: +1075/-24
  • I wish real life had a "Save" and "Load" button...
    • View Profile
Re: Trigonometric functions (tan, arctan) in axe
« Reply #1 on: November 15, 2011, 06:17:58 am »
A surprisingly accurate approximation for arctangent is:

Code: [Select]
atan(x) ≈ (π/2)*x/(abs(x)+1)
Which, when converted from radians, can be implemented in fixed point notation as x*64//(abs(x)+256) however this has overflow problems in the numerator.  Fortunately you can perform long division to reduce it to 64-(16384//(abs(x)+256)) which accounts for everything except the sign.  This can be done with a simple if statement:

Code: [Select]
:Lbl atan
:64-(16384//(abs(r1)+256))→r2
:If r1<<0
:  Return -r2
:End
:Return r2
« Last Edit: November 15, 2011, 10:17:45 pm by Quigibo »
___Axe_Parser___
Today the calculator, tomorrow the world!

Offline bilyp

  • LV2 Member (Next: 40)
  • **
  • Posts: 21
  • Rating: +9/-1
  • Axing the z80
    • View Profile
Re: Trigonometric functions (tan, arctan) in axe
« Reply #2 on: November 15, 2011, 07:34:51 pm »
 :o  Wow. That is perfect, exactly what I needed! Thank you for this, it answers my question!