124579Szliu /* 224579Szliu * Copyright (c) 1985 Regents of the University of California. 324579Szliu * 424579Szliu * Use and reproduction of this software are granted in accordance with 524579Szliu * the terms and conditions specified in the Berkeley Software License 624579Szliu * Agreement (in particular, this entails acknowledgement of the programs' 724579Szliu * source, and inclusion of this notice) with the additional understanding 824579Szliu * that all recipients should regard themselves as participants in an 924579Szliu * ongoing research project and hence should feel obligated to report 1024579Szliu * their experiences (good or bad) with these elementary function codes, 1124579Szliu * using "sendbug 4bsd-bugs@BERKELEY", to the authors. 1224579Szliu */ 1324579Szliu 1424579Szliu #ifndef lint 1524719Selefunt static char sccsid[] = 16*31991Szliu "@(#)cabs.c 1.2 (Berkeley) 8/21/85; 1.7 (ucb.elefunt) 08/03/87"; 1731855Szliu #endif /* not lint */ 1824579Szliu 1924579Szliu /* HYPOT(X,Y) 2024579Szliu * RETURN THE SQUARE ROOT OF X^2 + Y^2 WHERE Z=X+iY 2124579Szliu * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS) 2224579Szliu * CODED IN C BY K.C. NG, 11/28/84; 2324579Szliu * REVISED BY K.C. NG, 7/12/85. 2424579Szliu * 2524579Szliu * Required system supported functions : 2624579Szliu * copysign(x,y) 2724579Szliu * finite(x) 2824579Szliu * scalb(x,N) 2924579Szliu * sqrt(x) 3024579Szliu * 3124579Szliu * Method : 3224579Szliu * 1. replace x by |x| and y by |y|, and swap x and 3324579Szliu * y if y > x (hence x is never smaller than y). 3424579Szliu * 2. Hypot(x,y) is computed by: 3524579Szliu * Case I, x/y > 2 3624579Szliu * 3724579Szliu * y 3824579Szliu * hypot = x + ----------------------------- 3924579Szliu * 2 4024579Szliu * sqrt ( 1 + [x/y] ) + x/y 4124579Szliu * 4224579Szliu * Case II, x/y <= 2 4324579Szliu * y 4424579Szliu * hypot = x + -------------------------------------------------- 4524579Szliu * 2 4624579Szliu * [x/y] - 2 4724579Szliu * (sqrt(2)+1) + (x-y)/y + ----------------------------- 4824579Szliu * 2 4924579Szliu * sqrt ( 1 + [x/y] ) + sqrt(2) 5024579Szliu * 5124579Szliu * 5224579Szliu * 5324579Szliu * Special cases: 5424579Szliu * hypot(x,y) is INF if x or y is +INF or -INF; else 5524579Szliu * hypot(x,y) is NAN if x or y is NAN. 5624579Szliu * 5724579Szliu * Accuracy: 5824579Szliu * hypot(x,y) returns the sqrt(x^2+y^2) with error less than 1 ulps (units 5924579Szliu * in the last place). See Kahan's "Interval Arithmetic Options in the 6024579Szliu * Proposed IEEE Floating Point Arithmetic Standard", Interval Mathematics 6124579Szliu * 1980, Edited by Karl L.E. Nickel, pp 99-128. (A faster but less accurate 6224579Szliu * code follows in comments.) In a test run with 500,000 random arguments 6324579Szliu * on a VAX, the maximum observed error was .959 ulps. 6424579Szliu * 6524579Szliu * Constants: 6624579Szliu * The hexadecimal values are the intended ones for the following constants. 6724579Szliu * The decimal values may be used, provided that the compiler will convert 6824579Szliu * from decimal to binary accurately enough to produce the hexadecimal values 6924579Szliu * shown. 7024579Szliu */ 7124579Szliu 7231855Szliu #if defined(vax)||defined(tahoe) /* VAX D format */ 7331855Szliu #ifdef vax 7431814Szliu #define _0x(A,B) 0x/**/A/**/B 7531855Szliu #else /* vax */ 7631814Szliu #define _0x(A,B) 0x/**/B/**/A 7731855Szliu #endif /* vax */ 7824579Szliu /* static double */ 7924579Szliu /* r2p1hi = 2.4142135623730950345E0 , Hex 2^ 2 * .9A827999FCEF32 */ 8024579Szliu /* r2p1lo = 1.4349369327986523769E-17 , Hex 2^-55 * .84597D89B3754B */ 8124579Szliu /* sqrt2 = 1.4142135623730950622E0 ; Hex 2^ 1 * .B504F333F9DE65 */ 8231814Szliu static long r2p1hix[] = { _0x(8279,411a), _0x(ef32,99fc)}; 8331814Szliu static long r2p1lox[] = { _0x(597d,2484), _0x(754b,89b3)}; 8431814Szliu static long sqrt2x[] = { _0x(04f3,40b5), _0x(de65,33f9)}; 8524579Szliu #define r2p1hi (*(double*)r2p1hix) 8624579Szliu #define r2p1lo (*(double*)r2p1lox) 8724579Szliu #define sqrt2 (*(double*)sqrt2x) 8831855Szliu #else /* defined(vax)||defined(tahoe) */ 8924579Szliu static double 9024579Szliu r2p1hi = 2.4142135623730949234E0 , /*Hex 2^1 * 1.3504F333F9DE6 */ 9124579Szliu r2p1lo = 1.2537167179050217666E-16 , /*Hex 2^-53 * 1.21165F626CDD5 */ 9224579Szliu sqrt2 = 1.4142135623730951455E0 ; /*Hex 2^ 0 * 1.6A09E667F3BCD */ 9331855Szliu #endif /* defined(vax)||defined(tahoe) */ 9424579Szliu 95*31991Szliu double 96*31991Szliu hypot(x,y) 9724579Szliu double x, y; 9824579Szliu { 9924579Szliu static double zero=0, one=1, 10024579Szliu small=1.0E-18; /* fl(1+small)==1 */ 10124579Szliu static ibig=30; /* fl(1+2**(2*ibig))==1 */ 10224579Szliu double copysign(),scalb(),logb(),sqrt(),t,r; 10324579Szliu int finite(), exp; 10424579Szliu 10524579Szliu if(finite(x)) 10624579Szliu if(finite(y)) 10724579Szliu { 10824579Szliu x=copysign(x,one); 10924579Szliu y=copysign(y,one); 11024579Szliu if(y > x) 11124579Szliu { t=x; x=y; y=t; } 11224579Szliu if(x == zero) return(zero); 11324579Szliu if(y == zero) return(x); 11424579Szliu exp= logb(x); 11524579Szliu if(exp-(int)logb(y) > ibig ) 11624579Szliu /* raise inexact flag and return |x| */ 11724579Szliu { one+small; return(x); } 11824579Szliu 11924579Szliu /* start computing sqrt(x^2 + y^2) */ 12024579Szliu r=x-y; 12124579Szliu if(r>y) { /* x/y > 2 */ 12224579Szliu r=x/y; 12324579Szliu r=r+sqrt(one+r*r); } 12424579Szliu else { /* 1 <= x/y <= 2 */ 12524579Szliu r/=y; t=r*(r+2.0); 12624579Szliu r+=t/(sqrt2+sqrt(2.0+t)); 12724579Szliu r+=r2p1lo; r+=r2p1hi; } 12824579Szliu 12924579Szliu r=y/r; 13024579Szliu return(x+r); 13124579Szliu 13224579Szliu } 13324579Szliu 13424579Szliu else if(y==y) /* y is +-INF */ 13524579Szliu return(copysign(y,one)); 13624579Szliu else 13724579Szliu return(y); /* y is NaN and x is finite */ 13824579Szliu 13924579Szliu else if(x==x) /* x is +-INF */ 14024579Szliu return (copysign(x,one)); 14124579Szliu else if(finite(y)) 14224579Szliu return(x); /* x is NaN, y is finite */ 14331855Szliu #if !defined(vax)&&!defined(tahoe) 14424579Szliu else if(y!=y) return(y); /* x and y is NaN */ 14531855Szliu #endif /* !defined(vax)&&!defined(tahoe) */ 14624579Szliu else return(copysign(y,one)); /* y is INF */ 14724579Szliu } 14824579Szliu 149*31991Szliu /* CABS(Z) 150*31991Szliu * RETURN THE ABSOLUTE VALUE OF THE COMPLEX NUMBER Z = X + iY 151*31991Szliu * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS) 152*31991Szliu * CODED IN C BY K.C. NG, 11/28/84. 153*31991Szliu * REVISED BY K.C. NG, 7/12/85. 154*31991Szliu * 155*31991Szliu * Required kernel function : 156*31991Szliu * hypot(x,y) 157*31991Szliu * 158*31991Szliu * Method : 159*31991Szliu * cabs(z) = hypot(x,y) . 160*31991Szliu */ 161*31991Szliu 162*31991Szliu double 163*31991Szliu cabs(z) 164*31991Szliu struct { double x, y;} z; 165*31991Szliu { 166*31991Szliu return hypot(z.x,z.y); 167*31991Szliu } 168*31991Szliu 169*31991Szliu double 170*31991Szliu z_abs(z) 171*31991Szliu struct { double x,y;} *z; 172*31991Szliu { 173*31991Szliu return hypot(z->x,z->y); 174*31991Szliu } 175*31991Szliu 17624579Szliu /* A faster but less accurate version of cabs(x,y) */ 17724579Szliu #if 0 17824579Szliu double hypot(x,y) 17924579Szliu double x, y; 18024579Szliu { 18124579Szliu static double zero=0, one=1; 18224579Szliu small=1.0E-18; /* fl(1+small)==1 */ 18324579Szliu static ibig=30; /* fl(1+2**(2*ibig))==1 */ 18424579Szliu double copysign(),scalb(),logb(),sqrt(),temp; 18524579Szliu int finite(), exp; 18624579Szliu 18724579Szliu if(finite(x)) 18824579Szliu if(finite(y)) 18924579Szliu { 19024579Szliu x=copysign(x,one); 19124579Szliu y=copysign(y,one); 19224579Szliu if(y > x) 19324579Szliu { temp=x; x=y; y=temp; } 19424579Szliu if(x == zero) return(zero); 19524579Szliu if(y == zero) return(x); 19624579Szliu exp= logb(x); 19724579Szliu x=scalb(x,-exp); 19824579Szliu if(exp-(int)logb(y) > ibig ) 19924579Szliu /* raise inexact flag and return |x| */ 20024579Szliu { one+small; return(scalb(x,exp)); } 20124579Szliu else y=scalb(y,-exp); 20224579Szliu return(scalb(sqrt(x*x+y*y),exp)); 20324579Szliu } 20424579Szliu 20524579Szliu else if(y==y) /* y is +-INF */ 20624579Szliu return(copysign(y,one)); 20724579Szliu else 20824579Szliu return(y); /* y is NaN and x is finite */ 20924579Szliu 21024579Szliu else if(x==x) /* x is +-INF */ 21124579Szliu return (copysign(x,one)); 21224579Szliu else if(finite(y)) 21324579Szliu return(x); /* x is NaN, y is finite */ 21424579Szliu else if(y!=y) return(y); /* x and y is NaN */ 21524579Szliu else return(copysign(y,one)); /* y is INF */ 21624579Szliu } 21724579Szliu #endif 218