1 /* 2 * Copyright (c) 1985 Regents of the University of California. 3 * 4 * Use and reproduction of this software are granted in accordance with 5 * the terms and conditions specified in the Berkeley Software License 6 * Agreement (in particular, this entails acknowledgement of the programs' 7 * source, and inclusion of this notice) with the additional understanding 8 * that all recipients should regard themselves as participants in an 9 * ongoing research project and hence should feel obligated to report 10 * their experiences (good or bad) with these elementary function codes, 11 * using "sendbug 4bsd-bugs@BERKELEY", to the authors. 12 */ 13 14 #ifndef lint 15 static char sccsid[] = 16 "@(#)cabs.c 1.2 (Berkeley) 8/21/85; 1.2 (ucb.elefunt) 09/12/85"; 17 #endif not lint 18 19 /* CABS(Z) 20 * RETURN THE ABSOLUTE VALUE OF THE COMPLEX NUMBER Z = X + iY 21 * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS) 22 * CODED IN C BY K.C. NG, 11/28/84. 23 * REVISED BY K.C. NG, 7/12/85. 24 * 25 * Required kernel function : 26 * hypot(x,y) 27 * 28 * Method : 29 * cabs(z) = hypot(x,y) . 30 */ 31 32 double cabs(z) 33 struct { double x, y;} z; 34 { 35 double hypot(); 36 return(hypot(z.x,z.y)); 37 } 38 39 40 /* HYPOT(X,Y) 41 * RETURN THE SQUARE ROOT OF X^2 + Y^2 WHERE Z=X+iY 42 * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS) 43 * CODED IN C BY K.C. NG, 11/28/84; 44 * REVISED BY K.C. NG, 7/12/85. 45 * 46 * Required system supported functions : 47 * copysign(x,y) 48 * finite(x) 49 * scalb(x,N) 50 * sqrt(x) 51 * 52 * Method : 53 * 1. replace x by |x| and y by |y|, and swap x and 54 * y if y > x (hence x is never smaller than y). 55 * 2. Hypot(x,y) is computed by: 56 * Case I, x/y > 2 57 * 58 * y 59 * hypot = x + ----------------------------- 60 * 2 61 * sqrt ( 1 + [x/y] ) + x/y 62 * 63 * Case II, x/y <= 2 64 * y 65 * hypot = x + -------------------------------------------------- 66 * 2 67 * [x/y] - 2 68 * (sqrt(2)+1) + (x-y)/y + ----------------------------- 69 * 2 70 * sqrt ( 1 + [x/y] ) + sqrt(2) 71 * 72 * 73 * 74 * Special cases: 75 * hypot(x,y) is INF if x or y is +INF or -INF; else 76 * hypot(x,y) is NAN if x or y is NAN. 77 * 78 * Accuracy: 79 * hypot(x,y) returns the sqrt(x^2+y^2) with error less than 1 ulps (units 80 * in the last place). See Kahan's "Interval Arithmetic Options in the 81 * Proposed IEEE Floating Point Arithmetic Standard", Interval Mathematics 82 * 1980, Edited by Karl L.E. Nickel, pp 99-128. (A faster but less accurate 83 * code follows in comments.) In a test run with 500,000 random arguments 84 * on a VAX, the maximum observed error was .959 ulps. 85 * 86 * Constants: 87 * The hexadecimal values are the intended ones for the following constants. 88 * The decimal values may be used, provided that the compiler will convert 89 * from decimal to binary accurately enough to produce the hexadecimal values 90 * shown. 91 */ 92 93 #ifdef VAX /* VAX D format */ 94 /* static double */ 95 /* r2p1hi = 2.4142135623730950345E0 , Hex 2^ 2 * .9A827999FCEF32 */ 96 /* r2p1lo = 1.4349369327986523769E-17 , Hex 2^-55 * .84597D89B3754B */ 97 /* sqrt2 = 1.4142135623730950622E0 ; Hex 2^ 1 * .B504F333F9DE65 */ 98 static long r2p1hix[] = { 0x8279411a, 0xef3299fc}; 99 static long r2p1lox[] = { 0x597d2484, 0x754b89b3}; 100 static long sqrt2x[] = { 0x04f340b5, 0xde6533f9}; 101 #define r2p1hi (*(double*)r2p1hix) 102 #define r2p1lo (*(double*)r2p1lox) 103 #define sqrt2 (*(double*)sqrt2x) 104 #else /* IEEE double format */ 105 static double 106 r2p1hi = 2.4142135623730949234E0 , /*Hex 2^1 * 1.3504F333F9DE6 */ 107 r2p1lo = 1.2537167179050217666E-16 , /*Hex 2^-53 * 1.21165F626CDD5 */ 108 sqrt2 = 1.4142135623730951455E0 ; /*Hex 2^ 0 * 1.6A09E667F3BCD */ 109 #endif 110 111 double hypot(x,y) 112 double x, y; 113 { 114 static double zero=0, one=1, 115 small=1.0E-18; /* fl(1+small)==1 */ 116 static ibig=30; /* fl(1+2**(2*ibig))==1 */ 117 double copysign(),scalb(),logb(),sqrt(),t,r; 118 int finite(), exp; 119 120 if(finite(x)) 121 if(finite(y)) 122 { 123 x=copysign(x,one); 124 y=copysign(y,one); 125 if(y > x) 126 { t=x; x=y; y=t; } 127 if(x == zero) return(zero); 128 if(y == zero) return(x); 129 exp= logb(x); 130 if(exp-(int)logb(y) > ibig ) 131 /* raise inexact flag and return |x| */ 132 { one+small; return(x); } 133 134 /* start computing sqrt(x^2 + y^2) */ 135 r=x-y; 136 if(r>y) { /* x/y > 2 */ 137 r=x/y; 138 r=r+sqrt(one+r*r); } 139 else { /* 1 <= x/y <= 2 */ 140 r/=y; t=r*(r+2.0); 141 r+=t/(sqrt2+sqrt(2.0+t)); 142 r+=r2p1lo; r+=r2p1hi; } 143 144 r=y/r; 145 return(x+r); 146 147 } 148 149 else if(y==y) /* y is +-INF */ 150 return(copysign(y,one)); 151 else 152 return(y); /* y is NaN and x is finite */ 153 154 else if(x==x) /* x is +-INF */ 155 return (copysign(x,one)); 156 else if(finite(y)) 157 return(x); /* x is NaN, y is finite */ 158 else if(y!=y) return(y); /* x and y is NaN */ 159 else return(copysign(y,one)); /* y is INF */ 160 } 161 162 /* A faster but less accurate version of cabs(x,y) */ 163 #if 0 164 double hypot(x,y) 165 double x, y; 166 { 167 static double zero=0, one=1; 168 small=1.0E-18; /* fl(1+small)==1 */ 169 static ibig=30; /* fl(1+2**(2*ibig))==1 */ 170 double copysign(),scalb(),logb(),sqrt(),temp; 171 int finite(), exp; 172 173 if(finite(x)) 174 if(finite(y)) 175 { 176 x=copysign(x,one); 177 y=copysign(y,one); 178 if(y > x) 179 { temp=x; x=y; y=temp; } 180 if(x == zero) return(zero); 181 if(y == zero) return(x); 182 exp= logb(x); 183 x=scalb(x,-exp); 184 if(exp-(int)logb(y) > ibig ) 185 /* raise inexact flag and return |x| */ 186 { one+small; return(scalb(x,exp)); } 187 else y=scalb(y,-exp); 188 return(scalb(sqrt(x*x+y*y),exp)); 189 } 190 191 else if(y==y) /* y is +-INF */ 192 return(copysign(y,one)); 193 else 194 return(y); /* y is NaN and x is finite */ 195 196 else if(x==x) /* x is +-INF */ 197 return (copysign(x,one)); 198 else if(finite(y)) 199 return(x); /* x is NaN, y is finite */ 200 else if(y!=y) return(y); /* x and y is NaN */ 201 else return(copysign(y,one)); /* y is INF */ 202 } 203 #endif 204