1 /* 2 * Copyright (c) 1985 Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 static char sccsid[] = "@(#)pow.c 5.8 (Berkeley) 12/02/92"; 10 #endif /* not lint */ 11 12 /* POW(X,Y) 13 * RETURN X**Y 14 * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS) 15 * CODED IN C BY K.C. NG, 1/8/85; 16 * REVISED BY K.C. NG on 7/10/85. 17 * KERNEL pow_P() REPLACED BY P. McILROY 7/22/92. 18 * Required system supported functions: 19 * scalb(x,n) 20 * logb(x) 21 * copysign(x,y) 22 * finite(x) 23 * drem(x,y) 24 * 25 * Required kernel functions: 26 * exp__D(a,c) exp(a + c) for |a| << |c| 27 * struct d_double dlog(x) r.a + r.b, |r.b| < |r.a| 28 * 29 * Method 30 * 1. Compute and return log(x) in three pieces: 31 * log(x) = n*ln2 + hi + lo, 32 * where n is an integer. 33 * 2. Perform y*log(x) by simulating muti-precision arithmetic and 34 * return the answer in three pieces: 35 * y*log(x) = m*ln2 + hi + lo, 36 * where m is an integer. 37 * 3. Return x**y = exp(y*log(x)) 38 * = 2^m * ( exp(hi+lo) ). 39 * 40 * Special cases: 41 * (anything) ** 0 is 1 ; 42 * (anything) ** 1 is itself; 43 * (anything) ** NaN is NaN; 44 * NaN ** (anything except 0) is NaN; 45 * +(anything > 1) ** +INF is +INF; 46 * -(anything > 1) ** +INF is NaN; 47 * +-(anything > 1) ** -INF is +0; 48 * +-(anything < 1) ** +INF is +0; 49 * +(anything < 1) ** -INF is +INF; 50 * -(anything < 1) ** -INF is NaN; 51 * +-1 ** +-INF is NaN and signal INVALID; 52 * +0 ** +(anything except 0, NaN) is +0; 53 * -0 ** +(anything except 0, NaN, odd integer) is +0; 54 * +0 ** -(anything except 0, NaN) is +INF and signal DIV-BY-ZERO; 55 * -0 ** -(anything except 0, NaN, odd integer) is +INF with signal; 56 * -0 ** (odd integer) = -( +0 ** (odd integer) ); 57 * +INF ** +(anything except 0,NaN) is +INF; 58 * +INF ** -(anything except 0,NaN) is +0; 59 * -INF ** (odd integer) = -( +INF ** (odd integer) ); 60 * -INF ** (even integer) = ( +INF ** (even integer) ); 61 * -INF ** -(anything except integer,NaN) is NaN with signal; 62 * -(x=anything) ** (k=integer) is (-1)**k * (x ** k); 63 * -(anything except 0) ** (non-integer) is NaN with signal; 64 * 65 * Accuracy: 66 * pow(x,y) returns x**y nearly rounded. In particular, on a SUN, a VAX, 67 * and a Zilog Z8000, 68 * pow(integer,integer) 69 * always returns the correct integer provided it is representable. 70 * In a test run with 100,000 random arguments with 0 < x, y < 20.0 71 * on a VAX, the maximum observed error was 1.79 ulps (units in the 72 * last place). 73 * 74 * Constants : 75 * The hexadecimal values are the intended ones for the following constants. 76 * The decimal values may be used, provided that the compiler will convert 77 * from decimal to binary accurately enough to produce the hexadecimal values 78 * shown. 79 */ 80 81 #include <errno.h> 82 #include <math.h> 83 84 #include "mathimpl.h" 85 86 #if (defined(vax) || defined(tahoe)) 87 x = (double) (float) x 88 # define _IEEE 0 89 #else 90 # define _IEEE 1 91 # define TRUNC(x) *(((int *) &x)+1) &= 0xf8000000 92 # define infnan(x) ((x == EDOM)? zero/zero : ((x < 0) ? -one/zero : one/zero)) 93 #endif /* vax or tahoe */ 94 95 const static double zero=0.0, one=1.0, two=2.0, negone= -1.0; 96 97 static double pow_P __P((double, double)); 98 99 double pow(x,y) 100 double x,y; 101 { 102 double t; 103 if (y==zero) 104 return (one); 105 else if (y==one || (_IEEE && x != x)) 106 return (x); /* if x is NaN or y=1 */ 107 else if (_IEEE && y!=y) /* if y is NaN */ 108 return (y); 109 else if (!finite(y)) /* if y is INF */ 110 if ((t=fabs(x))==one) /* +-1 ** +-INF is NaN */ 111 return (y - y); 112 else if (t>one) 113 return ((y<0)? zero : ((x<zero)? y-y : y)); 114 else 115 return ((y>0)? zero : ((x<0)? y-y : -y)); 116 else if (y==two) 117 return (x*x); 118 else if (y==negone) 119 return (one/x); 120 /* x > 0, x == +0 */ 121 else if (copysign(one, x) == one) 122 return (pow_P(x, y)); 123 124 /* sign(x)= -1 */ 125 /* if y is an even integer */ 126 else if ( (t=drem(y,two)) == zero) 127 return (pow_P(-x, y)); 128 129 /* if y is an odd integer */ 130 else if (copysign(t,one) == one) 131 return (-pow_P(-x, y)); 132 133 /* Henceforth y is not an integer */ 134 else if (x==zero) /* x is -0 */ 135 return ((y>zero)? -x : one/(-x)); 136 else if (_IEEE) 137 return (zero/zero); 138 else 139 return (infnan(EDOM)); 140 } 141 /* kernel function for x >= 0 */ 142 static double 143 #ifdef _ANSI_SOURCE 144 pow_P(double x, double y) 145 #else 146 pow_P(x, y) double x, y; 147 #endif 148 { 149 struct Double s, t, log__D(); 150 double exp__D(), huge = 1e300, tiny = 1e-300; 151 152 if (x == 1) 153 return (one); 154 if (y >= 7e18) /* infinity */ 155 if (x < 1) 156 return(tiny*tiny); 157 else if (_IEEE) 158 return (huge*huge); 159 else 160 return (infnan(ERANGE)); 161 162 /* Return exp(y*log(x)), using simulated extended */ 163 /* precision for the log and the multiply. */ 164 165 s = log__D(x); 166 t.a = y; 167 TRUNC(t.a); 168 t.b = y - t.a; 169 t.b = s.b*y + t.b*s.a; 170 t.a *= s.a; 171 s.a = t.a + t.b; 172 s.b = (t.a - s.a) + t.b; 173 return (exp__D(s.a, s.b)); 174 } 175