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