Annotation of 43BSDReno/lib/libm/common/atan2.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * Copyright (c) 1985 Regents of the University of California.
                      3:  * All rights reserved.
                      4:  *
                      5:  * Redistribution and use in source and binary forms are permitted
                      6:  * provided that: (1) source distributions retain this entire copyright
                      7:  * notice and comment, and (2) distributions including binaries display
                      8:  * the following acknowledgement:  ``This product includes software
                      9:  * developed by the University of California, Berkeley and its contributors''
                     10:  * in the documentation or other materials provided with the distribution
                     11:  * and in all advertising materials mentioning features or use of this
                     12:  * software. Neither the name of the University nor the names of its
                     13:  * contributors may be used to endorse or promote products derived
                     14:  * from this software without specific prior written permission.
                     15:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
                     16:  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
                     17:  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
                     18:  *
                     19:  * All recipients should regard themselves as participants in an ongoing
                     20:  * research project and hence should feel obligated to report their
                     21:  * experiences (good or bad) with these elementary function codes, using
                     22:  * the sendbug(8) program, to the authors.
                     23:  */
                     24: 
                     25: #ifndef lint
                     26: static char sccsid[] = "@(#)atan2.c    5.5 (Berkeley) 6/1/90";
                     27: #endif /* not lint */
                     28: 
                     29: /* ATAN2(Y,X)
                     30:  * RETURN ARG (X+iY)
                     31:  * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
                     32:  * CODED IN C BY K.C. NG, 1/8/85; 
                     33:  * REVISED BY K.C. NG on 2/7/85, 2/13/85, 3/7/85, 3/30/85, 6/29/85.
                     34:  *
                     35:  * Required system supported functions :
                     36:  *     copysign(x,y)
                     37:  *     scalb(x,y)
                     38:  *     logb(x)
                     39:  *     
                     40:  * Method :
                     41:  *     1. Reduce y to positive by atan2(y,x)=-atan2(-y,x).
                     42:  *     2. Reduce x to positive by (if x and y are unexceptional): 
                     43:  *             ARG (x+iy) = arctan(y/x)           ... if x > 0,
                     44:  *             ARG (x+iy) = pi - arctan[y/(-x)]   ... if x < 0,
                     45:  *     3. According to the integer k=4t+0.25 truncated , t=y/x, the argument 
                     46:  *        is further reduced to one of the following intervals and the 
                     47:  *        arctangent of y/x is evaluated by the corresponding formula:
                     48:  *
                     49:  *         [0,7/16]       atan(y/x) = t - t^3*(a1+t^2*(a2+...(a10+t^2*a11)...)
                     50:  *        [7/16,11/16]    atan(y/x) = atan(1/2) + atan( (y-x/2)/(x+y/2) )
                     51:  *        [11/16.19/16]   atan(y/x) = atan( 1 ) + atan( (y-x)/(x+y) )
                     52:  *        [19/16,39/16]   atan(y/x) = atan(3/2) + atan( (y-1.5x)/(x+1.5y) )
                     53:  *        [39/16,INF]     atan(y/x) = atan(INF) + atan( -x/y )
                     54:  *
                     55:  * Special cases:
                     56:  * Notations: atan2(y,x) == ARG (x+iy) == ARG(x,y).
                     57:  *
                     58:  *     ARG( NAN , (anything) ) is NaN;
                     59:  *     ARG( (anything), NaN ) is NaN;
                     60:  *     ARG(+(anything but NaN), +-0) is +-0  ;
                     61:  *     ARG(-(anything but NaN), +-0) is +-PI ;
                     62:  *     ARG( 0, +-(anything but 0 and NaN) ) is +-PI/2;
                     63:  *     ARG( +INF,+-(anything but INF and NaN) ) is +-0 ;
                     64:  *     ARG( -INF,+-(anything but INF and NaN) ) is +-PI;
                     65:  *     ARG( +INF,+-INF ) is +-PI/4 ;
                     66:  *     ARG( -INF,+-INF ) is +-3PI/4;
                     67:  *     ARG( (anything but,0,NaN, and INF),+-INF ) is +-PI/2;
                     68:  *
                     69:  * Accuracy:
                     70:  *     atan2(y,x) returns (PI/pi) * the exact ARG (x+iy) nearly rounded, 
                     71:  *     where
                     72:  *
                     73:  *     in decimal:
                     74:  *             pi = 3.141592653589793 23846264338327 ..... 
                     75:  *    53 bits   PI = 3.141592653589793 115997963 ..... ,
                     76:  *    56 bits   PI = 3.141592653589793 227020265 ..... ,  
                     77:  *
                     78:  *     in hexadecimal:
                     79:  *             pi = 3.243F6A8885A308D313198A2E....
                     80:  *    53 bits   PI = 3.243F6A8885A30  =  2 * 1.921FB54442D18   error=.276ulps
                     81:  *    56 bits   PI = 3.243F6A8885A308 =  4 * .C90FDAA22168C2    error=.206ulps
                     82:  *     
                     83:  *     In a test run with 356,000 random argument on [-1,1] * [-1,1] on a
                     84:  *     VAX, the maximum observed error was 1.41 ulps (units of the last place)
                     85:  *     compared with (PI/pi)*(the exact ARG(x+iy)).
                     86:  *
                     87:  * Note:
                     88:  *     We use machine PI (the true pi rounded) in place of the actual
                     89:  *     value of pi for all the trig and inverse trig functions. In general, 
                     90:  *     if trig is one of sin, cos, tan, then computed trig(y) returns the 
                     91:  *     exact trig(y*pi/PI) nearly rounded; correspondingly, computed arctrig 
                     92:  *     returns the exact arctrig(y)*PI/pi nearly rounded. These guarantee the 
                     93:  *     trig functions have period PI, and trig(arctrig(x)) returns x for
                     94:  *     all critical values x.
                     95:  *     
                     96:  * Constants:
                     97:  * The hexadecimal values are the intended ones for the following constants.
                     98:  * The decimal values may be used, provided that the compiler will convert
                     99:  * from decimal to binary accurately enough to produce the hexadecimal values
                    100:  * shown.
                    101:  */
                    102: 
                    103: #include "mathimpl.h"
                    104: 
                    105: vc(athfhi, 4.6364760900080611433E-1  ,6338,3fed,da7b,2b0d,  -1, .ED63382B0DDA7B)
                    106: vc(athflo, 1.9338828231967579916E-19 ,5005,2164,92c0,9cfe, -62, .E450059CFE92C0)
                    107: vc(PIo4,   7.8539816339744830676E-1  ,0fda,4049,68c2,a221,   0, .C90FDAA22168C2)
                    108: vc(at1fhi, 9.8279372324732906796E-1  ,985e,407b,b4d9,940f,   0, .FB985E940FB4D9)
                    109: vc(at1flo,-3.5540295636764633916E-18 ,1edc,a383,eaea,34d6, -57,-.831EDC34D6EAEA)
                    110: vc(PIo2,   1.5707963267948966135E0   ,0fda,40c9,68c2,a221,   1, .C90FDAA22168C2)
                    111: vc(PI,     3.1415926535897932270E0   ,0fda,4149,68c2,a221,   2, .C90FDAA22168C2)
                    112: vc(a1,     3.3333333333333473730E-1  ,aaaa,3faa,ab75,aaaa,  -1, .AAAAAAAAAAAB75)
                    113: vc(a2,    -2.0000000000017730678E-1  ,cccc,bf4c,946e,cccd,  -2,-.CCCCCCCCCD946E)
                    114: vc(a3,     1.4285714286694640301E-1  ,4924,3f12,4262,9274,  -2, .92492492744262)
                    115: vc(a4,    -1.1111111135032672795E-1  ,8e38,bee3,6292,ebc6,  -3,-.E38E38EBC66292)
                    116: vc(a5,     9.0909091380563043783E-2  ,2e8b,3eba,d70c,b31b,  -3, .BA2E8BB31BD70C)
                    117: vc(a6,    -7.6922954286089459397E-2  ,89c8,be9d,7f18,27c3,  -3,-.9D89C827C37F18)
                    118: vc(a7,     6.6663180891693915586E-2  ,86b4,3e88,9e58,ae37,  -3, .8886B4AE379E58)
                    119: vc(a8,    -5.8772703698290408927E-2  ,bba5,be70,a942,8481,  -4,-.F0BBA58481A942)
                    120: vc(a9,     5.2170707402812969804E-2  ,b0f3,3e55,13ab,a1ab,  -4, .D5B0F3A1AB13AB)
                    121: vc(a10,   -4.4895863157820361210E-2  ,e4b9,be37,048f,7fd1,  -4,-.B7E4B97FD1048F)
                    122: vc(a11,    3.3006147437343875094E-2  ,3174,3e07,2d87,3cf7,  -4, .8731743CF72D87)
                    123: vc(a12,   -1.4614844866464185439E-2  ,731a,bd6f,76d9,2f34,  -6,-.EF731A2F3476D9)
                    124: 
                    125: ic(athfhi, 4.6364760900080609352E-1  ,  -2,  1.DAC670561BB4F)
                    126: ic(athflo, 4.6249969567426939759E-18 , -58,  1.5543B8F253271)
                    127: ic(PIo4,   7.8539816339744827900E-1  ,  -1,  1.921FB54442D18)
                    128: ic(at1fhi, 9.8279372324732905408E-1  ,  -1,  1.F730BD281F69B)
                    129: ic(at1flo,-2.4407677060164810007E-17 , -56, -1.C23DFEFEAE6B5)
                    130: ic(PIo2,   1.5707963267948965580E0   ,   0,  1.921FB54442D18)
                    131: ic(PI,     3.1415926535897931160E0   ,   1,  1.921FB54442D18)
                    132: ic(a1,     3.3333333333333942106E-1  ,  -2,  1.55555555555C3)
                    133: ic(a2,    -1.9999999999979536924E-1  ,  -3, -1.9999999997CCD)
                    134: ic(a3,     1.4285714278004377209E-1  ,  -3,  1.24924921EC1D7)
                    135: ic(a4,    -1.1111110579344973814E-1  ,  -4, -1.C71C7059AF280)
                    136: ic(a5,     9.0908906105474668324E-2  ,  -4,  1.745CE5AA35DB2)
                    137: ic(a6,    -7.6919217767468239799E-2  ,  -4, -1.3B0FA54BEC400)
                    138: ic(a7,     6.6614695906082474486E-2  ,  -4,  1.10DA924597FFF)
                    139: ic(a8,    -5.8358371008508623523E-2  ,  -5, -1.DE125FDDBD793)
                    140: ic(a9,     4.9850617156082015213E-2  ,  -5,  1.9860524BDD807)
                    141: ic(a10,   -3.6700606902093604877E-2  ,  -5, -1.2CA6C04C6937A)
                    142: ic(a11,    1.6438029044759730479E-2  ,  -6,  1.0D52174A1BB54)
                    143: 
                    144: #ifdef vccast
                    145: #define        athfhi  vccast(athfhi)
                    146: #define        athflo  vccast(athflo)
                    147: #define        PIo4    vccast(PIo4)
                    148: #define        at1fhi  vccast(at1fhi)
                    149: #define        at1flo  vccast(at1flo)
                    150: #define        PIo2    vccast(PIo2)
                    151: #define        PI      vccast(PI)
                    152: #define        a1      vccast(a1)
                    153: #define        a2      vccast(a2)
                    154: #define        a3      vccast(a3)
                    155: #define        a4      vccast(a4)
                    156: #define        a5      vccast(a5)
                    157: #define        a6      vccast(a6)
                    158: #define        a7      vccast(a7)
                    159: #define        a8      vccast(a8)
                    160: #define        a9      vccast(a9)
                    161: #define        a10     vccast(a10)
                    162: #define        a11     vccast(a11)
                    163: #define        a12     vccast(a12)
                    164: #endif
                    165: 
                    166: double atan2(y,x)
                    167: double  y,x;
                    168: {  
                    169:        static const double zero=0, one=1, small=1.0E-9, big=1.0E18;
                    170:        double t,z,signy,signx,hi,lo;
                    171:        int k,m;
                    172: 
                    173: #if !defined(vax)&&!defined(tahoe)
                    174:     /* if x or y is NAN */
                    175:        if(x!=x) return(x); if(y!=y) return(y);
                    176: #endif /* !defined(vax)&&!defined(tahoe) */
                    177: 
                    178:     /* copy down the sign of y and x */
                    179:        signy = copysign(one,y) ;  
                    180:        signx = copysign(one,x) ;  
                    181: 
                    182:     /* if x is 1.0, goto begin */
                    183:        if(x==1) { y=copysign(y,one); t=y; if(finite(t)) goto begin;}
                    184: 
                    185:     /* when y = 0 */
                    186:        if(y==zero) return((signx==one)?y:copysign(PI,signy));
                    187: 
                    188:     /* when x = 0 */
                    189:        if(x==zero) return(copysign(PIo2,signy));
                    190:            
                    191:     /* when x is INF */
                    192:        if(!finite(x))
                    193:            if(!finite(y)) 
                    194:                return(copysign((signx==one)?PIo4:3*PIo4,signy));
                    195:            else
                    196:                return(copysign((signx==one)?zero:PI,signy));
                    197: 
                    198:     /* when y is INF */
                    199:        if(!finite(y)) return(copysign(PIo2,signy));
                    200: 
                    201:     /* compute y/x */
                    202:        x=copysign(x,one); 
                    203:        y=copysign(y,one); 
                    204:        if((m=(k=logb(y))-logb(x)) > 60) t=big+big; 
                    205:            else if(m < -80 ) t=y/x;
                    206:            else { t = y/x ; y = scalb(y,-k); x=scalb(x,-k); }
                    207: 
                    208:     /* begin argument reduction */
                    209: begin:
                    210:        if (t < 2.4375) {                
                    211: 
                    212:        /* truncate 4(t+1/16) to integer for branching */
                    213:            k = 4 * (t+0.0625);
                    214:            switch (k) {
                    215: 
                    216:            /* t is in [0,7/16] */
                    217:            case 0:                    
                    218:            case 1:
                    219:                if (t < small) 
                    220:                    { big + small ;  /* raise inexact flag */
                    221:                      return (copysign((signx>zero)?t:PI-t,signy)); }
                    222: 
                    223:                hi = zero;  lo = zero;  break;
                    224: 
                    225:            /* t is in [7/16,11/16] */
                    226:            case 2:                    
                    227:                hi = athfhi; lo = athflo;
                    228:                z = x+x;
                    229:                t = ( (y+y) - x ) / ( z +  y ); break;
                    230: 
                    231:            /* t is in [11/16,19/16] */
                    232:            case 3:                    
                    233:            case 4:
                    234:                hi = PIo4; lo = zero;
                    235:                t = ( y - x ) / ( x + y ); break;
                    236: 
                    237:            /* t is in [19/16,39/16] */
                    238:            default:                   
                    239:                hi = at1fhi; lo = at1flo;
                    240:                z = y-x; y=y+y+y; t = x+x;
                    241:                t = ( (z+z)-x ) / ( t + y ); break;
                    242:            }
                    243:        }
                    244:        /* end of if (t < 2.4375) */
                    245: 
                    246:        else                           
                    247:        {
                    248:            hi = PIo2; lo = zero;
                    249: 
                    250:            /* t is in [2.4375, big] */
                    251:            if (t <= big)  t = - x / y;
                    252: 
                    253:            /* t is in [big, INF] */
                    254:            else          
                    255:              { big+small;      /* raise inexact flag */
                    256:                t = zero; }
                    257:        }
                    258:     /* end of argument reduction */
                    259: 
                    260:     /* compute atan(t) for t in [-.4375, .4375] */
                    261:        z = t*t;
                    262: #if defined(vax)||defined(tahoe)
                    263:        z = t*(z*(a1+z*(a2+z*(a3+z*(a4+z*(a5+z*(a6+z*(a7+z*(a8+
                    264:                        z*(a9+z*(a10+z*(a11+z*a12))))))))))));
                    265: #else  /* defined(vax)||defined(tahoe) */
                    266:        z = t*(z*(a1+z*(a2+z*(a3+z*(a4+z*(a5+z*(a6+z*(a7+z*(a8+
                    267:                        z*(a9+z*(a10+z*a11)))))))))));
                    268: #endif /* defined(vax)||defined(tahoe) */
                    269:        z = lo - z; z += t; z += hi;
                    270: 
                    271:        return(copysign((signx>zero)?z:PI-z,signy));
                    272: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.