home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / language / icon8_fx / atan2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-23  |  3.3 KB  |  131 lines

  1. /************************************************************************
  2.  *                                    *
  3.  *   Replacement atan2() function for PML library. Original function    *
  4.  *   from Fred Fish does not conform to Standard C.  It has arguments   *
  5.  *   in a reverse order.  It also does not complain if both arguments   *
  6.  *   are 0.0                                                            *
  7.  *                                    *
  8.  *   Michal Jaegermann - December 1990
  9.  *                                    *
  10.  ************************************************************************
  11.  */
  12. /************************************************************************
  13.  *                                    *
  14.  *                N O T I C E                *
  15.  *                                    *
  16.  *            Copyright Abandoned, 1987, Fred Fish        *
  17.  *                                    *
  18.  *    This previously copyrighted work has been placed into the    *
  19.  *    public domain by the author (Fred Fish) and may be freely used    *
  20.  *    for any purpose, private or commercial.  I would appreciate    *
  21.  *    it, as a courtesy, if this notice is left in all copies and    *
  22.  *    derivative works.  Thank you, and enjoy...            *
  23.  *                                    *
  24.  *    The author makes no warranty of any kind with respect to this    *
  25.  *    product and explicitly disclaims any implied warranties of    *
  26.  *    merchantability or fitness for any particular purpose.        *
  27.  *                                    *
  28.  ************************************************************************
  29.  */
  30.  
  31.  
  32. /*
  33.  *  FUNCTION
  34.  *
  35.  *    atan2   double precision arc tangent of two arguments
  36.  *
  37.  *  KEY WORDS
  38.  *
  39.  *    atan2
  40.  *    machine independent routines
  41.  *    trigonometric functions
  42.  *    math libraries
  43.  *
  44.  *  DESCRIPTION
  45.  *
  46.  *    Returns double precision arc tangent of two
  47.  *    double precision floating point arguments ( atan(Y/X) ).
  48.  *
  49.  *  USAGE
  50.  *
  51.  *    double atan2(y, x)
  52.  *    double y;
  53.  *    double x;
  54.  *
  55.  *  (This order of arguments really makes sense if you will notice
  56.  *   that this function is mainly used during a conversion from
  57.  *   rectangular to polar coordinates)
  58.  *
  59.  *  REFERENCES
  60.  *
  61.  *    Fortran 77 user's guide, Digital Equipment Corp. pp B-4.
  62.  *
  63.  *  RESTRICTIONS
  64.  *
  65.  *    For precision information refer to documentation of the
  66.  *    other floating point library routines called.
  67.  *    
  68.  *  PROGRAMMER
  69.  *
  70.  *    Fred Fish
  71.  *    Modified by Michal Jaegermann
  72.  *
  73.  *  INTERNALS
  74.  *
  75.  *    Computes atan2(y, x) from:
  76.  *
  77.  *        1.    If x = 0 and y != 0 then
  78.  *            atan2(y, x) = PI/2 * (sign(y))
  79.  *                      otherwise error signaled and
  80.  *                      atan2(y ,x) is 0.
  81.  *
  82.  *        2.    If x > 0 then
  83.  *            atan2(y, x) = atan(y/x)
  84.  *
  85.  *        3.    If x < 0 then atan2(y, x) =
  86.  *            PI*(sign(y)) + atan(y/x)
  87.  *
  88.  */
  89.  
  90. #include <stdio.h>
  91. #include <pmluser.h>
  92. #include "pml.h"
  93.  
  94. static char funcname[] = "atan2";
  95.  
  96. double atan2 (y, x)
  97. double y;
  98. double x;
  99. {
  100.     double result;
  101.     extern double sign();
  102.     extern double atan();
  103.     struct exception xcpt;
  104.  
  105.     ENTER ("atan2");
  106.     DEBUG4 ("atan2in", "x = %le y = %le", x, y);
  107.     if (x == 0.0) {
  108.     if (y == 0.0) {
  109.         result = 0.0;
  110.         xcpt.type = DOMAIN;
  111.         xcpt.name = funcname;
  112.         xcpt.arg1 = x;
  113.         xcpt.retval = result;
  114.         if (!matherr(&xcpt)) {
  115.         fprintf (stderr, "%s: DOMAIN error\n", funcname);
  116.         errno = EDOM;
  117.         }
  118.     } else {
  119.         result = sign (HALFPI, y);
  120.     }
  121.     } else {
  122.     result = atan (y/x);
  123.     if (x < 0.0) {
  124.         result += sign (PI, y);
  125.     }
  126.     }
  127.     DEBUG3 ("atan2out", "result %le", result);
  128.     LEAVE ();
  129.     return (result);
  130. }
  131.