|
|
1.1 ! root 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.5 (Berkeley) 8/21/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 7/10/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 double infnan(); ! 89: ! 90: /* double static */ ! 91: /* ln2hi = 6.9314718055829871446E-1 , Hex 2^ 0 * .B17217F7D00000 */ ! 92: /* ln2lo = 1.6465949582897081279E-12 , Hex 2^-39 * .E7BCD5E4F1D9CC */ ! 93: /* invln2 = 1.4426950408889634148E0 , Hex 2^ 1 * .B8AA3B295C17F1 */ ! 94: /* sqrt2 = 1.4142135623730950622E0 ; Hex 2^ 1 * .B504F333F9DE65 */ ! 95: static long ln2hix[] = { 0x72174031, 0x0000f7d0}; ! 96: static long ln2lox[] = { 0xbcd52ce7, 0xd9cce4f1}; ! 97: static long invln2x[] = { 0xaa3b40b8, 0x17f1295c}; ! 98: static long sqrt2x[] = { 0x04f340b5, 0xde6533f9}; ! 99: #define ln2hi (*(double*)ln2hix) ! 100: #define ln2lo (*(double*)ln2lox) ! 101: #define invln2 (*(double*)invln2x) ! 102: #define sqrt2 (*(double*)sqrt2x) ! 103: #else /* IEEE double */ ! 104: double static ! 105: ln2hi = 6.9314718036912381649E-1 , /*Hex 2^ -1 * 1.62E42FEE00000 */ ! 106: ln2lo = 1.9082149292705877000E-10 , /*Hex 2^-33 * 1.A39EF35793C76 */ ! 107: invln2 = 1.4426950408889633870E0 , /*Hex 2^ 0 * 1.71547652B82FE */ ! 108: sqrt2 = 1.4142135623730951455E0 ; /*Hex 2^ 0 * 1.6A09E667F3BCD */ ! 109: #endif ! 110: ! 111: double static zero=0.0, half=1.0/2.0, one=1.0, two=2.0, negone= -1.0; ! 112: ! 113: double pow(x,y) ! 114: double x,y; ! 115: { ! 116: double drem(),pow_p(),copysign(),t; ! 117: int finite(); ! 118: ! 119: if (y==zero) return(one); ! 120: else if(y==one ! 121: #ifndef VAX ! 122: ||x!=x ! 123: #endif ! 124: ) return( x ); /* if x is NaN or y=1 */ ! 125: #ifndef VAX ! 126: else if(y!=y) return( y ); /* if y is NaN */ ! 127: #endif ! 128: else if(!finite(y)) /* if y is INF */ ! 129: if((t=copysign(x,one))==one) return(zero/zero); ! 130: else if(t>one) return((y>zero)?y:zero); ! 131: else return((y<zero)?-y:zero); ! 132: else if(y==two) return(x*x); ! 133: else if(y==negone) return(one/x); ! 134: ! 135: /* sign(x) = 1 */ ! 136: else if(copysign(one,x)==one) return(pow_p(x,y)); ! 137: ! 138: /* sign(x)= -1 */ ! 139: /* if y is an even integer */ ! 140: else if ( (t=drem(y,two)) == zero) return( pow_p(-x,y) ); ! 141: ! 142: /* if y is an odd integer */ ! 143: else if (copysign(t,one) == one) return( -pow_p(-x,y) ); ! 144: ! 145: /* Henceforth y is not an integer */ ! 146: else if(x==zero) /* x is -0 */ ! 147: return((y>zero)?-x:one/(-x)); ! 148: else { /* return NaN */ ! 149: #ifdef VAX ! 150: return (infnan(EDOM)); /* NaN */ ! 151: #else /* IEEE double */ ! 152: return(zero/zero); ! 153: #endif ! 154: } ! 155: } ! 156: ! 157: /* pow_p(x,y) return x**y for x with sign=1 and finite y */ ! 158: static double pow_p(x,y) ! 159: double x,y; ! 160: { ! 161: double logb(),scalb(),copysign(),log__L(),exp__E(); ! 162: double c,s,t,z,tx,ty; ! 163: float sx,sy; ! 164: long k=0; ! 165: int n,m; ! 166: ! 167: if(x==zero||!finite(x)) { /* if x is +INF or +0 */ ! 168: #ifdef VAX ! 169: return((y>zero)?x:infnan(ERANGE)); /* if y<zero, return +INF */ ! 170: #else ! 171: return((y>zero)?x:one/x); ! 172: #endif ! 173: } ! 174: if(x==1.0) return(x); /* if x=1.0, return 1 since y is finite */ ! 175: ! 176: /* reduce x to z in [sqrt(1/2)-1, sqrt(2)-1] */ ! 177: z=scalb(x,-(n=logb(x))); ! 178: #ifndef VAX /* IEEE double */ /* subnormal number */ ! 179: if(n <= -1022) {n += (m=logb(z)); z=scalb(z,-m);} ! 180: #endif ! 181: if(z >= sqrt2 ) {n += 1; z *= half;} z -= one ; ! 182: ! 183: /* log(x) = nlog2+log(1+z) ~ nlog2 + t + tx */ ! 184: s=z/(two+z); c=z*z*half; tx=s*(c+log__L(s*s)); ! 185: t= z-(c-tx); tx += (z-t)-c; ! 186: ! 187: /* if y*log(x) is neither too big nor too small */ ! 188: if((s=logb(y)+logb(n+t)) < 12.0) ! 189: if(s>-60.0) { ! 190: ! 191: /* compute y*log(x) ~ mlog2 + t + c */ ! 192: s=y*(n+invln2*t); ! 193: m=s+copysign(half,s); /* m := nint(y*log(x)) */ ! 194: k=y; ! 195: if((double)k==y) { /* if y is an integer */ ! 196: k = m-k*n; ! 197: sx=t; tx+=(t-sx); } ! 198: else { /* if y is not an integer */ ! 199: k =m; ! 200: tx+=n*ln2lo; ! 201: sx=(c=n*ln2hi)+t; tx+=(c-sx)+t; } ! 202: /* end of checking whether k==y */ ! 203: ! 204: sy=y; ty=y-sy; /* y ~ sy + ty */ ! 205: s=(double)sx*sy-k*ln2hi; /* (sy+ty)*(sx+tx)-kln2 */ ! 206: z=(tx*ty-k*ln2lo); ! 207: tx=tx*sy; ty=sx*ty; ! 208: t=ty+z; t+=tx; t+=s; ! 209: c= -((((t-s)-tx)-ty)-z); ! 210: ! 211: /* return exp(y*log(x)) */ ! 212: t += exp__E(t,c); return(scalb(one+t,m)); ! 213: } ! 214: /* end of if log(y*log(x)) > -60.0 */ ! 215: ! 216: else ! 217: /* exp(+- tiny) = 1 with inexact flag */ ! 218: {ln2hi+ln2lo; return(one);} ! 219: else if(copysign(one,y)*(n+invln2*t) <zero) ! 220: /* exp(-(big#)) underflows to zero */ ! 221: return(scalb(one,-5000)); ! 222: else ! 223: /* exp(+(big#)) overflows to INF */ ! 224: return(scalb(one, 5000)); ! 225: ! 226: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.