atan2.c raw

   1  /* origin: FreeBSD /usr/src/lib/msun/src/e_atan2.c */
   2  /*
   3   * ====================================================
   4   * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
   5   *
   6   * Developed at SunSoft, a Sun Microsystems, Inc. business.
   7   * Permission to use, copy, modify, and distribute this
   8   * software is freely granted, provided that this notice
   9   * is preserved.
  10   * ====================================================
  11   *
  12   */
  13  /* atan2(y,x)
  14   * Method :
  15   *      1. Reduce y to positive by atan2(y,x)=-atan2(-y,x).
  16   *      2. Reduce x to positive by (if x and y are unexceptional):
  17   *              ARG (x+iy) = arctan(y/x)           ... if x > 0,
  18   *              ARG (x+iy) = pi - arctan[y/(-x)]   ... if x < 0,
  19   *
  20   * Special cases:
  21   *
  22   *      ATAN2((anything), NaN ) is NaN;
  23   *      ATAN2(NAN , (anything) ) is NaN;
  24   *      ATAN2(+-0, +(anything but NaN)) is +-0  ;
  25   *      ATAN2(+-0, -(anything but NaN)) is +-pi ;
  26   *      ATAN2(+-(anything but 0 and NaN), 0) is +-pi/2;
  27   *      ATAN2(+-(anything but INF and NaN), +INF) is +-0 ;
  28   *      ATAN2(+-(anything but INF and NaN), -INF) is +-pi;
  29   *      ATAN2(+-INF,+INF ) is +-pi/4 ;
  30   *      ATAN2(+-INF,-INF ) is +-3pi/4;
  31   *      ATAN2(+-INF, (anything but,0,NaN, and INF)) is +-pi/2;
  32   *
  33   * Constants:
  34   * The hexadecimal values are the intended ones for the following
  35   * constants. The decimal values may be used, provided that the
  36   * compiler will convert from decimal to binary accurately enough
  37   * to produce the hexadecimal values shown.
  38   */
  39  
  40  #include "libm.h"
  41  
  42  static const double
  43  pi     = 3.1415926535897931160E+00, /* 0x400921FB, 0x54442D18 */
  44  pi_lo  = 1.2246467991473531772E-16; /* 0x3CA1A626, 0x33145C07 */
  45  
  46  double atan2(double y, double x)
  47  {
  48  	double z;
  49  	uint32_t m,lx,ly,ix,iy;
  50  
  51  	if (isnan(x) || isnan(y))
  52  		return x+y;
  53  	EXTRACT_WORDS(ix, lx, x);
  54  	EXTRACT_WORDS(iy, ly, y);
  55  	if ((ix-0x3ff00000 | lx) == 0)  /* x = 1.0 */
  56  		return atan(y);
  57  	m = ((iy>>31)&1) | ((ix>>30)&2);  /* 2*sign(x)+sign(y) */
  58  	ix = ix & 0x7fffffff;
  59  	iy = iy & 0x7fffffff;
  60  
  61  	/* when y = 0 */
  62  	if ((iy|ly) == 0) {
  63  		switch(m) {
  64  		case 0:
  65  		case 1: return y;   /* atan(+-0,+anything)=+-0 */
  66  		case 2: return  pi; /* atan(+0,-anything) = pi */
  67  		case 3: return -pi; /* atan(-0,-anything) =-pi */
  68  		}
  69  	}
  70  	/* when x = 0 */
  71  	if ((ix|lx) == 0)
  72  		return m&1 ? -pi/2 : pi/2;
  73  	/* when x is INF */
  74  	if (ix == 0x7ff00000) {
  75  		if (iy == 0x7ff00000) {
  76  			switch(m) {
  77  			case 0: return  pi/4;   /* atan(+INF,+INF) */
  78  			case 1: return -pi/4;   /* atan(-INF,+INF) */
  79  			case 2: return  3*pi/4; /* atan(+INF,-INF) */
  80  			case 3: return -3*pi/4; /* atan(-INF,-INF) */
  81  			}
  82  		} else {
  83  			switch(m) {
  84  			case 0: return  0.0; /* atan(+...,+INF) */
  85  			case 1: return -0.0; /* atan(-...,+INF) */
  86  			case 2: return  pi;  /* atan(+...,-INF) */
  87  			case 3: return -pi;  /* atan(-...,-INF) */
  88  			}
  89  		}
  90  	}
  91  	/* |y/x| > 0x1p64 */
  92  	if (ix+(64<<20) < iy || iy == 0x7ff00000)
  93  		return m&1 ? -pi/2 : pi/2;
  94  
  95  	/* z = atan(|y/x|) without spurious underflow */
  96  	if ((m&2) && iy+(64<<20) < ix)  /* |y/x| < 0x1p-64, x<0 */
  97  		z = 0;
  98  	else
  99  		z = atan(fabs(y/x));
 100  	switch (m) {
 101  	case 0: return z;              /* atan(+,+) */
 102  	case 1: return -z;             /* atan(-,+) */
 103  	case 2: return pi - (z-pi_lo); /* atan(+,-) */
 104  	default: /* case 3 */
 105  		return (z-pi_lo) - pi; /* atan(-,-) */
 106  	}
 107  }
 108