Hi, This code uses a lookup table and is accurate to +/-1 degree (-180 < X <= +180 degrees). I am sure you can tweak it to improve the accuracy.
Regards Luke const short int ATAN_LUT[65] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 10, 11, 12, 13, 14, 14, 15, 16, 17, 18, 18, 19, 20, 21, 22, 22, 23, 24, 25, 25, 26, 27, 27, 28, 29, 30, 30, 31, 32, 32, 33, 33, 34, 35, 35, 36, 36, 37, 37, 38, 39, 39, 40, 40, 41, 41, 42, 42, 43, 43, 44, 44, 45, 45 }; const int SCALE_UP_BITS = 6; // scale up 6 bits (i.e., 64) //---------------------------------------------------------------------- ---- // Description : calculate the arctangent (i.e., atan(y_number/x_number)) // Parameters : y_number = y coordinate of the point // x_number = x coordinate of the point // Assumptions : short int = 16 bits // Return : the arctangent in degree between 180 and -180, // excluding -180. // If y_number and x_number are both 0, 0 will be return. // The resolution is +/- 1 degree. //---------------------------------------------------------------------- ---- short int atan2(short int y_number, short int x_number) { // check if (y_number == 0) if (0 == y_number) { return (x_number < 0 ? 180 : 0); } // get the absolute value of x_number and y_number int x_number_abs = x_number > 0 ? x_number : 0 - x_number; int y_number_abs = y_number > 0 ? y_number : 0 - y_number; // get the arctangent in the first quadrant // // do y_number_abs/x_number_abs if y_number_abs < x_number_abs // else do x_number_abs/y_number_abs // because ATAN LUT is from 0 degree to 45 degree short int result; if(y_number_abs < x_number_abs) { result = ATAN_LUT[(y_number_abs << SCALE_UP_BITS) / x_number_abs]; } else { result = 90 - ATAN_LUT[(x_number_abs << SCALE_UP_BITS) / y_number_abs]; } // calculate the final arctangent if (x_number > 0) { if (y_number < 0) { return (0 - result); } return result; } if (y_number < 0) { return (-180 + result); } return (180 - result); } -----Original Message----- From: Dmitry [mailto:di...@eis.ru] Sent: Monday, 17 March 2003 4:28 PM To: mspgcc-users@lists.sourceforge.net Subject: Re: [Mspgcc-users] who knows the best atan() approximation? I need about 0.5 degree tolerance within [0,45] degrees range. However, need more precise results at about 45 degrees point. ~d On Monday 17 March 2003 10:56, Garst R. Reese wrote: > Dmitry wrote: > > Fellows, > > who knows the best atan() approx for argument within [0,1[ (return angles > > from 0 to 45 degrees)? > > How much error can you tolerate? > > x / (1 + .28*x*x) |e(x)| <= 5e-3 > > Garst > > > ------------------------------------------------------- > This SF.net email is sponsored by:Crypto Challenge is now open! > Get cracking and register here for some mind boggling fun and > the chance of winning an Apple iPod: > http://ads.sourceforge.net/cgi-bin/redirect.pl?thaw0031en > _______________________________________________ > Mspgcc-users mailing list > Mspgcc-users@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/mspgcc-users -- /******************************************************************** ("`-''-/").___..--''"`-._ (\ Dimmy the Wild UA1ACZ `6_ 6 ) `-. ( ).`-.__.`) Enterprise Information Sys (_Y_.)' ._ ) `._ `. ``-..-' Nevsky prospekt, 20 / 44 _..`--'_..-_/ /--'_.' ,' Saint Petersburg, Russia (il),-'' (li),' ((!.-' +7 (812) 3468202, 5585314 ********************************************************************/ ------------------------------------------------------- This SF.net email is sponsored by:Crypto Challenge is now open! Get cracking and register here for some mind boggling fun and the chance of winning an Apple iPod: http://ads.sourceforge.net/cgi-bin/redirect.pl?thaw0031en _______________________________________________ Mspgcc-users mailing list Mspgcc-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/mspgcc-users This email is confidential and intended solely for the use of the individual to whom it is addressed. Any views or opinions presented are solely those of the author and do not necessarily represent those of Nautronix Ltd. If you are not the intended recipient, you have received this email in error and use, dissemination, forwarding, printing, or copying of this email is strictly prohibited. If you have received this email in error please contact the sender. Although our computer systems use active virus protection software, and we take various measures to reduce the risk of viruses being transmitted in e-mail messages and attachments sent from this company, we cannot guarantee that such e-mail messages and attachments are free from viruses on receipt. It is a condition of our using e-mail to correspond with you, that any and all liability on our part arising directly or indirectly out of any virus is excluded. Please ensure that you run virus checking software on all e-mail messages and attachments before reading them.