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[] = "@(#)pow.c 4.4 (Berkeley) 06/03/85"; 16 #endif not lint 17 18 /* POW(X,Y) 19 * RETURN X**Y 20 * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS) 21 * CODED IN C BY K.C. NG, 1/8/85; 22 * REVISED BY K.C. NG on 5/12/85. 23 * 24 * Required system supported functions: 25 * scalb(x,n) 26 * logb(x) 27 * copysign(x,y) 28 * finite(x) 29 * drem(x,y) 30 * 31 * Required kernel functions: 32 * exp__E(a,c) ...return exp(a+c) - 1 - a*a/2 33 * log__L(x) ...return (log(1+x) - 2s)/s, s=x/(2+x) 34 * pow_p(x,y) ...return +(anything)**(finite non zero) 35 * 36 * Method 37 * 1. Compute and return log(x) in three pieces: 38 * log(x) = n*ln2 + hi + lo, 39 * where n is an integer. 40 * 2. Perform y*log(x) by simulating muti-precision arithmetic and 41 * return the answer in three pieces: 42 * y*log(x) = m*ln2 + hi + lo, 43 * where m is an integer. 44 * 3. Return x**y = exp(y*log(x)) 45 * = 2^m * ( exp(hi+lo) ). 46 * 47 * Special cases: 48 * (anything) ** 0 is 1 ; 49 * (anything) ** 1 is itself; 50 * (anything) ** NAN is NAN; 51 * NAN ** (anything except 0) is NAN; 52 * +-(anything > 1) ** +INF is +INF; 53 * +-(anything > 1) ** -INF is +0; 54 * +-(anything < 1) ** +INF is +0; 55 * +-(anything < 1) ** -INF is +INF; 56 * +-1 ** +-INF is NAN and signal INVALID; 57 * +0 ** +(anything except 0, NAN) is +0; 58 * -0 ** +(anything except 0, NAN, odd integer) is +0; 59 * +0 ** -(anything except 0, NAN) is +INF and signal DIV-BY-ZERO; 60 * -0 ** -(anything except 0, NAN, odd integer) is +INF with signal; 61 * -0 ** (odd integer) = -( +0 ** (odd integer) ); 62 * +INF ** +(anything except 0,NAN) is +INF; 63 * +INF ** -(anything except 0,NAN) is +0; 64 * -INF ** (odd integer) = -( +INF ** (odd integer) ); 65 * -INF ** (even integer) = ( +INF ** (even integer) ); 66 * -INF ** -(anything except integer,NAN) is NAN with signal; 67 * -(x=anything) ** (k=integer) is (-1)**k * (x ** k); 68 * -(anything except 0) ** (non-integer) is NAN with signal; 69 * 70 * Accuracy: 71 * pow(x,y) returns x**y nearly rounded. In particular, on a SUN, a VAX, 72 * and a Zilog Z8000, 73 * pow(integer,integer) 74 * always returns the correct integer provided it is representable. 75 * In a test run with 100,000 random arguments with 0 < x, y < 20.0 76 * on a VAX, the maximum observed error was 1.79 ulps (units in the 77 * last place). 78 * 79 * Constants : 80 * The hexadecimal values are the intended ones for the following constants. 81 * The decimal values may be used, provided that the compiler will convert 82 * from decimal to binary accurately enough to produce the hexadecimal values 83 * shown. 84 */ 85 86 #ifdef VAX /* VAX D format */ 87 #include <errno.h> 88 extern errno; 89 static long NaN_[] = {0x8000, 0x0}; 90 #define NaN (*(double *) NaN_) 91 92 /* double static */ 93 /* ln2hi = 6.9314718055829871446E-1 , Hex 2^ 0 * .B17217F7D00000 */ 94 /* ln2lo = 1.6465949582897081279E-12 , Hex 2^-39 * .E7BCD5E4F1D9CC */ 95 /* invln2 = 1.4426950408889634148E0 , Hex 2^ 1 * .B8AA3B295C17F1 */ 96 /* sqrt2 = 1.4142135623730950622E0 ; Hex 2^ 1 * .B504F333F9DE65 */ 97 static long ln2hix[] = { 0x72174031, 0x0000f7d0}; 98 static long ln2lox[] = { 0xbcd52ce7, 0xd9cce4f1}; 99 static long invln2x[] = { 0xaa3b40b8, 0x17f1295c}; 100 static long sqrt2x[] = { 0x04f340b5, 0xde6533f9}; 101 #define ln2hi (*(double*)ln2hix) 102 #define ln2lo (*(double*)ln2lox) 103 #define invln2 (*(double*)invln2x) 104 #define sqrt2 (*(double*)sqrt2x) 105 #else /* IEEE double format */ 106 double static 107 ln2hi = 6.9314718036912381649E-1 , /*Hex 2^ -1 * 1.62E42FEE00000 */ 108 ln2lo = 1.9082149292705877000E-10 , /*Hex 2^-33 * 1.A39EF35793C76 */ 109 invln2 = 1.4426950408889633870E0 , /*Hex 2^ 0 * 1.71547652B82FE */ 110 sqrt2 = 1.4142135623730951455E0 ; /*Hex 2^ 0 * 1.6A09E667F3BCD */ 111 #endif 112 113 double static zero=0.0, half=1.0/2.0, one=1.0, two=2.0, negone= -1.0; 114 115 double pow(x,y) 116 double x,y; 117 { 118 double drem(),pow_p(),copysign(),t; 119 int finite(); 120 121 if (y==zero) return(one); 122 else if(y==one||x!=x) return( x ); /* if x is NAN or y=1 */ 123 else if(y!=y) return( y ); /* if y is NAN */ 124 else if(!finite(y)) /* if y is INF */ 125 if((t=copysign(x,one))==one) return(zero/zero); 126 else if(t>one) return((y>zero)?y:zero); 127 else return((y<zero)?-y:zero); 128 else if(y==two) return(x*x); 129 else if(y==negone) return(one/x); 130 131 /* sign(x) = 1 */ 132 else if(copysign(one,x)==one) return(pow_p(x,y)); 133 134 /* sign(x)= -1 */ 135 /* if y is an even integer */ 136 else if ( (t=drem(y,two)) == zero) return( pow_p(-x,y) ); 137 138 /* if y is an odd integer */ 139 else if (copysign(t,one) == one) return( -pow_p(-x,y) ); 140 141 /* Henceforth y is not an integer */ 142 else if(x==zero) /* x is -0 */ 143 return((y>zero)?-x:one/(-x)); 144 else { /* return NAN */ 145 #ifdef VAX 146 errno = EDOM; 147 return (NaN); 148 #else 149 return(zero/zero); 150 #endif 151 } 152 } 153 154 /* pow_p(x,y) return x**y for x with sign=1 and finite y */ 155 static double pow_p(x,y) 156 double x,y; 157 { 158 double logb(),scalb(),copysign(),log__L(),exp__E(); 159 double c,s,t,z,tx,ty; 160 float sx,sy; 161 long k=0; 162 int n,m; 163 164 if(x==zero||!finite(x)) { /* if x is +INF or +0 */ 165 #ifdef VAX 166 if (y<zero) errno = ERANGE; 167 #endif 168 return((y>zero)?x:one/x); 169 } 170 171 /* reduce x to z in [sqrt(1/2)-1, sqrt(2)-1] */ 172 z=scalb(x,-(n=logb(x))); 173 #ifndef VAX /* subnormal number */ 174 if(n <= -1022) {n += (m=logb(z)); z=scalb(z,-m);} 175 #endif 176 if(z >= sqrt2 ) {n += 1; z *= half;} z -= one ; 177 178 /* log(x) = nlog2+log(1+z) ~ nlog2 + t + tx */ 179 s=z/(two+z); c=z*z*half; tx=s*(c+log__L(s*s)); 180 t= z-(c-tx); tx += (z-t)-c; 181 182 /* if y*log(x) is neither too big nor too small */ 183 if((s=logb(y)+logb(n+t)) < 12.0) 184 if(s>-60.0) { 185 186 /* compute y*log(x) ~ mlog2 + t + c */ 187 s=y*(n+invln2*t); 188 m=s+copysign(half,s); /* m := nint(y*log(x)) */ 189 k=y; 190 if((double)k==y) { /* if y is an integer */ 191 k = m-k*n; 192 sx=t; tx+=(t-sx); } 193 else { /* if y is not an integer */ 194 k =m; 195 tx+=n*ln2lo; 196 sx=(c=n*ln2hi)+t; tx+=(c-sx)+t; } 197 /* end of checking whether k==y */ 198 199 sy=y; ty=y-sy; /* y ~ sy + ty */ 200 s=(double)sx*sy-k*ln2hi; /* (sy+ty)*(sx+tx)-kln2 */ 201 z=(tx*ty-k*ln2lo); 202 tx=tx*sy; ty=sx*ty; 203 t=ty+z; t+=tx; t+=s; 204 c= -((((t-s)-tx)-ty)-z); 205 206 /* return exp(y*log(x)) */ 207 t += exp__E(t,c); return(scalb(one+t,m)); 208 } 209 /* end of if log(y*log(x)) > -60.0 */ 210 211 else 212 /* exp(+- tiny) = 1 */ 213 return(one); 214 else if(copysign(one,y)*(n+invln2*t) <zero) 215 /* exp(-(big#)) underflows to zero */ 216 return(scalb(one,-5000)); 217 else 218 /* exp(+(big#)) overflows to INF */ 219 return(scalb(one, 5000)); 220 221 } 222