home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / astrnomy / de118i.zip / ZATAN2.C < prev   
C/C++ Source or Header  |  1991-05-18  |  1KB  |  93 lines

  1. /*                            atan2()
  2.  *
  3.  *    Quadrant correct inverse circular tangent
  4.  *
  5.  *
  6.  *
  7.  * SYNOPSIS:
  8.  *
  9.  * double x, y, z, atan2();
  10.  *
  11.  * z = atan2( x, y );
  12.  *
  13.  *
  14.  *
  15.  * DESCRIPTION:
  16.  *
  17.  * Returns radian angle between 0 and +2pi whose tangent
  18.  * is y/x.
  19.  *
  20.  *
  21.  *
  22.  * ACCURACY:
  23.  *
  24.  * See atan.c.
  25.  *
  26.  */
  27.  
  28.  
  29. /*
  30. Cephes Math Library Release 2.0:  April, 1987
  31. Copyright 1984, 1987 by Stephen L. Moshier
  32. Direct inquiries to 30 Frost Street, Cambridge, MA 02140
  33. Certain routines from the Library, including this one, may
  34. be used and distributed freely provided this notice is retained
  35. and source code is included with all distributions.
  36. */
  37.  
  38. #include "prec.h"
  39.  
  40. extern DOUBLE PI, Two, OneandaHalf, Half, Zero;
  41.  
  42. DOUBLE zatan2( x, y )
  43. DOUBLE x, y;
  44. {
  45. DOUBLE z, w;
  46. short code;
  47. DOUBLE ATAN();
  48.  
  49.  
  50. code = 0;
  51.  
  52. if( x < 0 )
  53.     code = 2;
  54. if( y < 0 )
  55.     code |= 1;
  56.  
  57. if( x == 0 )
  58.     {
  59.     if( code & 1 )
  60.         return( OneandaHalf*PI );
  61.     if( y == Zero )
  62.         return( Zero );
  63.     return( Half*PI );
  64.     }
  65.  
  66. if( y == 0 )
  67.     {
  68.     if( code & 2 )
  69.         return( PI );
  70.     return( Zero );
  71.     }
  72.  
  73.  
  74. if( code == 0 )
  75.     w = Zero;
  76. else if( code == 1 )
  77.     w = Two*PI;
  78. else
  79.     w = PI;
  80. /*
  81. switch( code )
  82.     {
  83.     case 0: w = 0.0; break;
  84.     case 1: w = 2.0 * PI; break;
  85.     case 2:
  86.     case 3: w = PI; break;
  87.     }
  88. */
  89. z = ATAN( y/x );
  90.  
  91. return( w + z );
  92. }
  93.