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 15*24719Selefunt static char sccsid[] = 16*24719Selefunt "@(#)cabs.c 1.2 (Berkeley) 8/21/85; 1.2 (ucb.elefunt) 09/12/85"; 1724579Szliu #endif not lint 1824579Szliu 1924579Szliu /* CABS(Z) 2024579Szliu * RETURN THE ABSOLUTE VALUE OF THE COMPLEX NUMBER 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 kernel function : 2624579Szliu * hypot(x,y) 2724579Szliu * 2824579Szliu * Method : 2924579Szliu * cabs(z) = hypot(x,y) . 3024579Szliu */ 3124579Szliu 3224579Szliu double cabs(z) 3324579Szliu struct { double x, y;} z; 3424579Szliu { 3524579Szliu double hypot(); 3624579Szliu return(hypot(z.x,z.y)); 3724579Szliu } 3824579Szliu 3924579Szliu 4024579Szliu /* HYPOT(X,Y) 4124579Szliu * RETURN THE SQUARE ROOT OF X^2 + Y^2 WHERE Z=X+iY 4224579Szliu * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS) 4324579Szliu * CODED IN C BY K.C. NG, 11/28/84; 4424579Szliu * REVISED BY K.C. NG, 7/12/85. 4524579Szliu * 4624579Szliu * Required system supported functions : 4724579Szliu * copysign(x,y) 4824579Szliu * finite(x) 4924579Szliu * scalb(x,N) 5024579Szliu * sqrt(x) 5124579Szliu * 5224579Szliu * Method : 5324579Szliu * 1. replace x by |x| and y by |y|, and swap x and 5424579Szliu * y if y > x (hence x is never smaller than y). 5524579Szliu * 2. Hypot(x,y) is computed by: 5624579Szliu * Case I, x/y > 2 5724579Szliu * 5824579Szliu * y 5924579Szliu * hypot = x + ----------------------------- 6024579Szliu * 2 6124579Szliu * sqrt ( 1 + [x/y] ) + x/y 6224579Szliu * 6324579Szliu * Case II, x/y <= 2 6424579Szliu * y 6524579Szliu * hypot = x + -------------------------------------------------- 6624579Szliu * 2 6724579Szliu * [x/y] - 2 6824579Szliu * (sqrt(2)+1) + (x-y)/y + ----------------------------- 6924579Szliu * 2 7024579Szliu * sqrt ( 1 + [x/y] ) + sqrt(2) 7124579Szliu * 7224579Szliu * 7324579Szliu * 7424579Szliu * Special cases: 7524579Szliu * hypot(x,y) is INF if x or y is +INF or -INF; else 7624579Szliu * hypot(x,y) is NAN if x or y is NAN. 7724579Szliu * 7824579Szliu * Accuracy: 7924579Szliu * hypot(x,y) returns the sqrt(x^2+y^2) with error less than 1 ulps (units 8024579Szliu * in the last place). See Kahan's "Interval Arithmetic Options in the 8124579Szliu * Proposed IEEE Floating Point Arithmetic Standard", Interval Mathematics 8224579Szliu * 1980, Edited by Karl L.E. Nickel, pp 99-128. (A faster but less accurate 8324579Szliu * code follows in comments.) In a test run with 500,000 random arguments 8424579Szliu * on a VAX, the maximum observed error was .959 ulps. 8524579Szliu * 8624579Szliu * Constants: 8724579Szliu * The hexadecimal values are the intended ones for the following constants. 8824579Szliu * The decimal values may be used, provided that the compiler will convert 8924579Szliu * from decimal to binary accurately enough to produce the hexadecimal values 9024579Szliu * shown. 9124579Szliu */ 9224579Szliu 9324579Szliu #ifdef VAX /* VAX D format */ 9424579Szliu /* static double */ 9524579Szliu /* r2p1hi = 2.4142135623730950345E0 , Hex 2^ 2 * .9A827999FCEF32 */ 9624579Szliu /* r2p1lo = 1.4349369327986523769E-17 , Hex 2^-55 * .84597D89B3754B */ 9724579Szliu /* sqrt2 = 1.4142135623730950622E0 ; Hex 2^ 1 * .B504F333F9DE65 */ 9824579Szliu static long r2p1hix[] = { 0x8279411a, 0xef3299fc}; 9924579Szliu static long r2p1lox[] = { 0x597d2484, 0x754b89b3}; 10024579Szliu static long sqrt2x[] = { 0x04f340b5, 0xde6533f9}; 10124579Szliu #define r2p1hi (*(double*)r2p1hix) 10224579Szliu #define r2p1lo (*(double*)r2p1lox) 10324579Szliu #define sqrt2 (*(double*)sqrt2x) 10424579Szliu #else /* IEEE double format */ 10524579Szliu static double 10624579Szliu r2p1hi = 2.4142135623730949234E0 , /*Hex 2^1 * 1.3504F333F9DE6 */ 10724579Szliu r2p1lo = 1.2537167179050217666E-16 , /*Hex 2^-53 * 1.21165F626CDD5 */ 10824579Szliu sqrt2 = 1.4142135623730951455E0 ; /*Hex 2^ 0 * 1.6A09E667F3BCD */ 10924579Szliu #endif 11024579Szliu 11124579Szliu double hypot(x,y) 11224579Szliu double x, y; 11324579Szliu { 11424579Szliu static double zero=0, one=1, 11524579Szliu small=1.0E-18; /* fl(1+small)==1 */ 11624579Szliu static ibig=30; /* fl(1+2**(2*ibig))==1 */ 11724579Szliu double copysign(),scalb(),logb(),sqrt(),t,r; 11824579Szliu int finite(), exp; 11924579Szliu 12024579Szliu if(finite(x)) 12124579Szliu if(finite(y)) 12224579Szliu { 12324579Szliu x=copysign(x,one); 12424579Szliu y=copysign(y,one); 12524579Szliu if(y > x) 12624579Szliu { t=x; x=y; y=t; } 12724579Szliu if(x == zero) return(zero); 12824579Szliu if(y == zero) return(x); 12924579Szliu exp= logb(x); 13024579Szliu if(exp-(int)logb(y) > ibig ) 13124579Szliu /* raise inexact flag and return |x| */ 13224579Szliu { one+small; return(x); } 13324579Szliu 13424579Szliu /* start computing sqrt(x^2 + y^2) */ 13524579Szliu r=x-y; 13624579Szliu if(r>y) { /* x/y > 2 */ 13724579Szliu r=x/y; 13824579Szliu r=r+sqrt(one+r*r); } 13924579Szliu else { /* 1 <= x/y <= 2 */ 14024579Szliu r/=y; t=r*(r+2.0); 14124579Szliu r+=t/(sqrt2+sqrt(2.0+t)); 14224579Szliu r+=r2p1lo; r+=r2p1hi; } 14324579Szliu 14424579Szliu r=y/r; 14524579Szliu return(x+r); 14624579Szliu 14724579Szliu } 14824579Szliu 14924579Szliu else if(y==y) /* y is +-INF */ 15024579Szliu return(copysign(y,one)); 15124579Szliu else 15224579Szliu return(y); /* y is NaN and x is finite */ 15324579Szliu 15424579Szliu else if(x==x) /* x is +-INF */ 15524579Szliu return (copysign(x,one)); 15624579Szliu else if(finite(y)) 15724579Szliu return(x); /* x is NaN, y is finite */ 15824579Szliu else if(y!=y) return(y); /* x and y is NaN */ 15924579Szliu else return(copysign(y,one)); /* y is INF */ 16024579Szliu } 16124579Szliu 16224579Szliu /* A faster but less accurate version of cabs(x,y) */ 16324579Szliu #if 0 16424579Szliu double hypot(x,y) 16524579Szliu double x, y; 16624579Szliu { 16724579Szliu static double zero=0, one=1; 16824579Szliu small=1.0E-18; /* fl(1+small)==1 */ 16924579Szliu static ibig=30; /* fl(1+2**(2*ibig))==1 */ 17024579Szliu double copysign(),scalb(),logb(),sqrt(),temp; 17124579Szliu int finite(), exp; 17224579Szliu 17324579Szliu if(finite(x)) 17424579Szliu if(finite(y)) 17524579Szliu { 17624579Szliu x=copysign(x,one); 17724579Szliu y=copysign(y,one); 17824579Szliu if(y > x) 17924579Szliu { temp=x; x=y; y=temp; } 18024579Szliu if(x == zero) return(zero); 18124579Szliu if(y == zero) return(x); 18224579Szliu exp= logb(x); 18324579Szliu x=scalb(x,-exp); 18424579Szliu if(exp-(int)logb(y) > ibig ) 18524579Szliu /* raise inexact flag and return |x| */ 18624579Szliu { one+small; return(scalb(x,exp)); } 18724579Szliu else y=scalb(y,-exp); 18824579Szliu return(scalb(sqrt(x*x+y*y),exp)); 18924579Szliu } 19024579Szliu 19124579Szliu else if(y==y) /* y is +-INF */ 19224579Szliu return(copysign(y,one)); 19324579Szliu else 19424579Szliu return(y); /* y is NaN and x is finite */ 19524579Szliu 19624579Szliu else if(x==x) /* x is +-INF */ 19724579Szliu return (copysign(x,one)); 19824579Szliu else if(finite(y)) 19924579Szliu return(x); /* x is NaN, y is finite */ 20024579Szliu else if(y!=y) return(y); /* x and y is NaN */ 20124579Szliu else return(copysign(y,one)); /* y is INF */ 20224579Szliu } 20324579Szliu #endif 204