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

unix.superglobalmegacorp.com

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