base/math/e_atan2.c

Go to the documentation of this file.
00001 /* @(#)e_atan2.c 5.1 93/09/24 */ 00002 /* 00003 * ==================================================== 00004 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 00005 * 00006 * Developed at SunPro, a Sun Microsystems, Inc. business. 00007 * Permission to use, copy, modify, and distribute this 00008 * software is freely granted, provided that this notice 00009 * is preserved. 00010 * ==================================================== 00011 */ 00012 00013 #if defined(LIBM_SCCS) && !defined(lint) 00014 static char rcsid[] = "$NetBSD: e_atan2.c,v 1.8 1995/05/10 20:44:51 jtc Exp $"; 00015 #endif 00016 00017 /* __ieee754_atan2(y,x) 00018 * Method : 00019 * 1. Reduce y to positive by atan2(y,x)=-atan2(-y,x). 00020 * 2. Reduce x to positive by (if x and y are unexceptional): 00021 * ARG (x+iy) = arctan(y/x) ... if x > 0, 00022 * ARG (x+iy) = pi - arctan[y/(-x)] ... if x < 0, 00023 * 00024 * Special cases: 00025 * 00026 * ATAN2((anything), NaN ) is NaN; 00027 * ATAN2(NAN , (anything) ) is NaN; 00028 * ATAN2(+-0, +(anything but NaN)) is +-0 ; 00029 * ATAN2(+-0, -(anything but NaN)) is +-pi ; 00030 * ATAN2(+-(anything but 0 and NaN), 0) is +-pi/2; 00031 * ATAN2(+-(anything but INF and NaN), +INF) is +-0 ; 00032 * ATAN2(+-(anything but INF and NaN), -INF) is +-pi; 00033 * ATAN2(+-INF,+INF ) is +-pi/4 ; 00034 * ATAN2(+-INF,-INF ) is +-3pi/4; 00035 * ATAN2(+-INF, (anything but,0,NaN, and INF)) is +-pi/2; 00036 * 00037 * Constants: 00038 * The hexadecimal values are the intended ones for the following 00039 * constants. The decimal values may be used, provided that the 00040 * compiler will convert from decimal to binary accurately enough 00041 * to produce the hexadecimal values shown. 00042 */ 00043 00044 #include "math.h" 00045 #include "mathP.h" 00046 00047 #ifdef __STDC__ 00048 static const double 00049 #else 00050 static double 00051 #endif 00052 tiny = 1.0e-300, 00053 zero = 0.0, 00054 pi_o_4 = 7.8539816339744827900E-01, /* 0x3FE921FB, 0x54442D18 */ 00055 pi_o_2 = 1.5707963267948965580E+00, /* 0x3FF921FB, 0x54442D18 */ 00056 pi = 3.1415926535897931160E+00, /* 0x400921FB, 0x54442D18 */ 00057 pi_lo = 1.2246467991473531772E-16; /* 0x3CA1A626, 0x33145C07 */ 00058 00059 #ifdef __STDC__ 00060 double __ieee754_atan2(double y, double x) 00061 #else 00062 double __ieee754_atan2(y,x) 00063 double y,x; 00064 #endif 00065 { 00066 double z; 00067 int32_t k,m,hx,hy,ix,iy; 00068 u_int32_t lx,ly; 00069 00070 EXTRACT_WORDS(hx,lx,x); 00071 ix = hx&0x7fffffff; 00072 EXTRACT_WORDS(hy,ly,y); 00073 iy = hy&0x7fffffff; 00074 if(((ix|((lx|-lx)>>31))>0x7ff00000)|| 00075 ((iy|((ly|-ly)>>31))>0x7ff00000)) /* x or y is NaN */ 00076 return x+y; 00077 if((hx-(0x3ff00000|lx))==0) return atan(y); /* x=1.0 */ 00078 m = ((hy>>31)&1)|((hx>>30)&2); /* 2*sign(x)+sign(y) */ 00079 00080 /* when y = 0 */ 00081 if((iy|ly)==0) { 00082 switch(m) { 00083 case 0: 00084 case 1: return y; /* atan(+-0,+anything)=+-0 */ 00085 case 2: return pi+tiny;/* atan(+0,-anything) = pi */ 00086 case 3: return -pi-tiny;/* atan(-0,-anything) =-pi */ 00087 } 00088 } 00089 /* when x = 0 */ 00090 if((ix|lx)==0) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny; 00091 00092 /* when x is INF */ 00093 if(ix==0x7ff00000) { 00094 if(iy==0x7ff00000) { 00095 switch(m) { 00096 case 0: return pi_o_4+tiny;/* atan(+INF,+INF) */ 00097 case 1: return -pi_o_4-tiny;/* atan(-INF,+INF) */ 00098 case 2: return 3.0*pi_o_4+tiny;/*atan(+INF,-INF)*/ 00099 case 3: return -3.0*pi_o_4-tiny;/*atan(-INF,-INF)*/ 00100 } 00101 } else { 00102 switch(m) { 00103 case 0: return zero ; /* atan(+...,+INF) */ 00104 case 1: return -zero ; /* atan(-...,+INF) */ 00105 case 2: return pi+tiny ; /* atan(+...,-INF) */ 00106 case 3: return -pi-tiny ; /* atan(-...,-INF) */ 00107 } 00108 } 00109 } 00110 /* when y is INF */ 00111 if(iy==0x7ff00000) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny; 00112 00113 /* compute y/x */ 00114 k = (iy-ix)>>20; 00115 if(k > 60) z=pi_o_2+0.5*pi_lo; /* |y/x| > 2**60 */ 00116 else if(hx<0&&k<-60) z=0.0; /* |y|/x < -2**60 */ 00117 else z=atan(fabs(y/x)); /* safe to do y/x */ 00118 switch (m) { 00119 case 0: return z ; /* atan(+,+) */ 00120 case 1: { 00121 u_int32_t zh; 00122 GET_HIGH_WORD(zh,z); 00123 SET_HIGH_WORD(z,zh ^ 0x80000000); 00124 } 00125 return z ; /* atan(-,+) */ 00126 case 2: return pi-(z-pi_lo);/* atan(+,-) */ 00127 default: /* case 3 */ 00128 return (z-pi_lo)-pi;/* atan(-,-) */ 00129 } 00130 }

Generated on Thu Nov 20 11:49:51 2008 for RTAI API by doxygen 1.3.8