Author Topic: Inverse Cosine of an 8.8 number  (Read 2902 times)

0 Members and 1 Guest are viewing this topic.

Offline LincolnB

  • Check It Out Now
  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1115
  • Rating: +125/-4
  • By Hackers For Hackers
    • View Profile
Inverse Cosine of an 8.8 number
« on: February 21, 2012, 05:09:38 pm »
Two questions: What is the format for an 8.8 number in Axe, and can you take the inverse cosine of an 8.8 number?
Completed Projects:
   >> Spacky Emprise   >> Spacky 2 - Beta   >> Fantastic Sam
   >> An Exercise In Futility   >> GeoCore

My Current Projects:

Projects in Development:
In Medias Res - Contest Entry

Talk to me if you need help with Axe coding.

Spoiler For Bragging Rights:
Not much yet, hopefully this section will grow soon with time (and more contests)

Offline geekygenius

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 17
  • Rating: +4/-0
    • View Profile
Re: Inverse Cosine of an 8.8 number
« Reply #1 on: March 18, 2012, 09:12:25 pm »
I don't know much about floating point numbers in axe, but maybe try going back to the unit circle.

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Inverse Cosine of an 8.8 number
« Reply #2 on: March 18, 2012, 09:22:53 pm »
You might want to look into the taylor expansion of inverse-cosine

But out of curiosity, and with the potential to solve this issue with vectors, what do you need inverse cosine for?

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: Inverse Cosine of an 8.8 number
« Reply #3 on: March 18, 2012, 11:00:02 pm »
To answer the question of doing arbitrarily complicated "advanced" math in Axe (A system which does not support many math functions) is to approximate using more simple functions.  This is in fact what Axe does natively for sin, cos, arctan, etc.  I will demonstrate how I would approach this problem, the same way I did those other functions.  Hopefully this will be helpful to everyone.  Take a look at how arccos looks:

First things first, since we are using 8.8 and brads (binary radians) the same picture should range from -256 to 256 left to right, and 256 to 0 up to down.

Next, does the shape look like anything simple that you're familiar with?  What if we split it into 2 mirror images at the center?  Yes!  The left and right halves look like rotated and/or flipped square root functions, close enough!

So what are the functions for left and right?  Lets do the right side first.  By the shape, we know its going to take the form of sqrt(-x).  But its shifted to the right by 256 so sqrt(256-x) and also we want it to go through point (0,128) and so when x=0, sqrt(256-x) should be 128, but its actually just 16.  So multiply by 8 and finally you get y = 8*sqrt(256-x) for the right half.  Follow a similar process for the left and you get: y = 256 - 8*sqrt(256+x)

One cool thing to notice is that in fixed point, all math is modulo 256 and so 256.0 is actually the same as 0.0 so these equations strangely optimize to a case where it looks like we're taking the square root of a negative number all the time, but that's just modular arithmetic for you!  This also forces us to add an extra special case.

Code: [Select]
y = 8.0*sqrt(-x) when x>0
y = -8.0*sqrt(x) when x<0
y = 128.0 when x=0

So now the code should be easy and straightforward.  Here is an unoptimized version for readability:

Code: [Select]
:Lbl acos
:If r1=0.0
:  Return 128.0
:ElseIf r1<<0.0
:  Return -√(r1)r*8.0
:  Return √(-r1)r*8.0

This is all untested, but I believe it should work  :)
« Last Edit: March 18, 2012, 11:04:20 pm by Quigibo »
Today the calculator, tomorrow the world!