134123Sbostic /* 224579Szliu * Copyright (c) 1985 Regents of the University of California. 334123Sbostic * All rights reserved. 434123Sbostic * 534123Sbostic * Redistribution and use in source and binary forms are permitted 634925Sbostic * provided that the above copyright notice and this paragraph are 734925Sbostic * duplicated in all such forms and that any documentation, 834925Sbostic * advertising materials, and other materials related to such 934925Sbostic * distribution and use acknowledge that the software was developed 1034925Sbostic * by the University of California, Berkeley. The name of the 1134925Sbostic * University may not be used to endorse or promote products derived 1234925Sbostic * from this software without specific prior written permission. 1334925Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1434925Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1534925Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1634123Sbostic * 1734123Sbostic * All recipients should regard themselves as participants in an ongoing 1834123Sbostic * research project and hence should feel obligated to report their 1934123Sbostic * experiences (good or bad) with these elementary function codes, using 2034123Sbostic * the sendbug(8) program, to the authors. 2124579Szliu */ 2224579Szliu 2324579Szliu #ifndef lint 24*35681Sbostic static char sccsid[] = "@(#)cabs.c 5.4 (Berkeley) 09/22/88"; 2534123Sbostic #endif /* not lint */ 2624579Szliu 2724579Szliu /* HYPOT(X,Y) 2824579Szliu * RETURN THE SQUARE ROOT OF X^2 + Y^2 WHERE Z=X+iY 2924579Szliu * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS) 3024579Szliu * CODED IN C BY K.C. NG, 11/28/84; 3124579Szliu * REVISED BY K.C. NG, 7/12/85. 3224579Szliu * 3324579Szliu * Required system supported functions : 3424579Szliu * copysign(x,y) 3524579Szliu * finite(x) 3624579Szliu * scalb(x,N) 3724579Szliu * sqrt(x) 3824579Szliu * 3924579Szliu * Method : 4024579Szliu * 1. replace x by |x| and y by |y|, and swap x and 4124579Szliu * y if y > x (hence x is never smaller than y). 4224579Szliu * 2. Hypot(x,y) is computed by: 4324579Szliu * Case I, x/y > 2 4424579Szliu * 4524579Szliu * y 4624579Szliu * hypot = x + ----------------------------- 4724579Szliu * 2 4824579Szliu * sqrt ( 1 + [x/y] ) + x/y 4924579Szliu * 5024579Szliu * Case II, x/y <= 2 5124579Szliu * y 5224579Szliu * hypot = x + -------------------------------------------------- 5324579Szliu * 2 5424579Szliu * [x/y] - 2 5524579Szliu * (sqrt(2)+1) + (x-y)/y + ----------------------------- 5624579Szliu * 2 5724579Szliu * sqrt ( 1 + [x/y] ) + sqrt(2) 5824579Szliu * 5924579Szliu * 6024579Szliu * 6124579Szliu * Special cases: 6224579Szliu * hypot(x,y) is INF if x or y is +INF or -INF; else 6324579Szliu * hypot(x,y) is NAN if x or y is NAN. 6424579Szliu * 6524579Szliu * Accuracy: 6624579Szliu * hypot(x,y) returns the sqrt(x^2+y^2) with error less than 1 ulps (units 6724579Szliu * in the last place). See Kahan's "Interval Arithmetic Options in the 6824579Szliu * Proposed IEEE Floating Point Arithmetic Standard", Interval Mathematics 6924579Szliu * 1980, Edited by Karl L.E. Nickel, pp 99-128. (A faster but less accurate 7024579Szliu * code follows in comments.) In a test run with 500,000 random arguments 7124579Szliu * on a VAX, the maximum observed error was .959 ulps. 7224579Szliu * 7324579Szliu * Constants: 7424579Szliu * The hexadecimal values are the intended ones for the following constants. 7524579Szliu * The decimal values may be used, provided that the compiler will convert 7624579Szliu * from decimal to binary accurately enough to produce the hexadecimal values 7724579Szliu * shown. 7824579Szliu */ 79*35681Sbostic #include "mathimpl.h" 8024579Szliu 81*35681Sbostic vc(r2p1hi, 2.4142135623730950345E0 ,8279,411a,ef32,99fc, 2, .9A827999FCEF32) 82*35681Sbostic vc(r2p1lo, 1.4349369327986523769E-17 ,597d,2484,754b,89b3, -55, .84597D89B3754B) 83*35681Sbostic vc(sqrt2, 1.4142135623730950622E0 ,04f3,40b5,de65,33f9, 1, .B504F333F9DE65) 8424579Szliu 85*35681Sbostic ic(r2p1hi, 2.4142135623730949234E0 , 1, 1.3504F333F9DE6) 86*35681Sbostic ic(r2p1lo, 1.2537167179050217666E-16 , -53, 1.21165F626CDD5) 87*35681Sbostic ic(sqrt2, 1.4142135623730951455E0 , 0, 1.6A09E667F3BCD) 88*35681Sbostic 89*35681Sbostic #ifdef vccast 90*35681Sbostic #define r2p1hi vccast(r2p1hi) 91*35681Sbostic #define r2p1lo vccast(r2p1lo) 92*35681Sbostic #define sqrt2 vccast(sqrt2) 93*35681Sbostic #endif 94*35681Sbostic 9531991Szliu double 9631991Szliu hypot(x,y) 9724579Szliu double x, y; 9824579Szliu { 99*35681Sbostic static const double zero=0, one=1, 10024579Szliu small=1.0E-18; /* fl(1+small)==1 */ 101*35681Sbostic static const ibig=30; /* fl(1+2**(2*ibig))==1 */ 102*35681Sbostic double t,r; 103*35681Sbostic int 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 14931991Szliu /* CABS(Z) 15031991Szliu * RETURN THE ABSOLUTE VALUE OF THE COMPLEX NUMBER Z = X + iY 15131991Szliu * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS) 15231991Szliu * CODED IN C BY K.C. NG, 11/28/84. 15331991Szliu * REVISED BY K.C. NG, 7/12/85. 15431991Szliu * 15531991Szliu * Required kernel function : 15631991Szliu * hypot(x,y) 15731991Szliu * 15831991Szliu * Method : 15931991Szliu * cabs(z) = hypot(x,y) . 16031991Szliu */ 16131991Szliu 16231991Szliu double 16331991Szliu cabs(z) 16431991Szliu struct { double x, y;} z; 16531991Szliu { 16631991Szliu return hypot(z.x,z.y); 16731991Szliu } 16831991Szliu 16931991Szliu double 17031991Szliu z_abs(z) 17131991Szliu struct { double x,y;} *z; 17231991Szliu { 17331991Szliu return hypot(z->x,z->y); 17431991Szliu } 17531991Szliu 17624579Szliu /* A faster but less accurate version of cabs(x,y) */ 17724579Szliu #if 0 17824579Szliu double hypot(x,y) 17924579Szliu double x, y; 18024579Szliu { 181*35681Sbostic static const double zero=0, one=1; 18224579Szliu small=1.0E-18; /* fl(1+small)==1 */ 183*35681Sbostic static const ibig=30; /* fl(1+2**(2*ibig))==1 */ 184*35681Sbostic double temp; 185*35681Sbostic int 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