Magic numbers in atan2

34 views
Skip to first unread message

akshay...@gmail.com

unread,
Apr 15, 2019, 8:11:53 AM4/15/19
to libfixmath
Hi,
This code is a great starting point.

Premise:
I am planning to use this code in one of our projects. The current library is implementation of 16.16 fixed point. The controller that we are using it on is 16-bit, which executes 32-bit operations at a low pace. Additionally the compiler does not support 64-bit number at all (which is a requirement of div function). Hence we wish to translate entire code to either 8.8 or 4.12 fixed point (based on our system's resolution need).

Current state:
I have already written square root function for both 4.12 and 8.8 fixed point, which is based on this code. It works perfect for our current resolution need.

Query:
There are 2 magic numbers in atan2 -> 0x00003240 and 0x0000FB50. Could someone explain what they stand for? Additionally, if I could get the literature/source algorithm/equations of trigonometric functions used in the code, it would be great help.

--

With regards,

Akshay

 

While starting job, think, “If I can’t, who can?”

But when you have finished, be polit enough to say, “If I can, who can’t?”

Petteri Aimonen

unread,
Apr 15, 2019, 12:37:51 PM4/15/19
to libfi...@googlegroups.com
Hi,

> There are 2 magic numbers in atan2 -> 0x00003240 and 0x0000FB50. Could
> someone explain what they stand for? Additionally, if I could get the
> literature/source algorithm/equations of trigonometric functions used in
> the code, it would be great help.

angle = fix16_mul(0x00003240 , r_3) - fix16_mul(0x0000FB50,r) +
PI_DIV_4;

That looks like a third order polynomial fit to atan2() in some range.
The factors (0x3240/65536 = 0.19, 0xFB50/65536=0.98) are close to Taylor
series approximation (0.33, 1.0), but while Taylor series offers best
fit at x = 0, polynomial fit offers better fit over some defined range.

I do not know exactly what range the polynomial has been optimized to
work over.

--
Petteri

akshay...@gmail.com

unread,
Apr 16, 2019, 1:13:45 AM4/16/19
to libfixmath
To answer my own question, here is the equation used for atan2:
Reply all
Reply to author
Forward
0 new messages