1 /* $NetBSD: n_pow.c,v 1.4 1999/07/02 15:37:37 simonb Exp $ */ 2 /* 3 * Copyright (c) 1985, 1993 4 * The Regents of the University of California. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by the University of 17 * California, Berkeley and its contributors. 18 * 4. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #ifndef lint 36 #if 0 37 static char sccsid[] = "@(#)pow.c 8.1 (Berkeley) 6/4/93"; 38 #endif 39 #endif /* not lint */ 40 41 /* POW(X,Y) 42 * RETURN X**Y 43 * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS) 44 * CODED IN C BY K.C. NG, 1/8/85; 45 * REVISED BY K.C. NG on 7/10/85. 46 * KERNEL pow_P() REPLACED BY P. McILROY 7/22/92. 47 * Required system supported functions: 48 * scalb(x,n) 49 * logb(x) 50 * copysign(x,y) 51 * finite(x) 52 * drem(x,y) 53 * 54 * Required kernel functions: 55 * exp__D(a,c) exp(a + c) for |a| << |c| 56 * struct d_double dlog(x) r.a + r.b, |r.b| < |r.a| 57 * 58 * Method 59 * 1. Compute and return log(x) in three pieces: 60 * log(x) = n*ln2 + hi + lo, 61 * where n is an integer. 62 * 2. Perform y*log(x) by simulating muti-precision arithmetic and 63 * return the answer in three pieces: 64 * y*log(x) = m*ln2 + hi + lo, 65 * where m is an integer. 66 * 3. Return x**y = exp(y*log(x)) 67 * = 2^m * ( exp(hi+lo) ). 68 * 69 * Special cases: 70 * (anything) ** 0 is 1 ; 71 * (anything) ** 1 is itself; 72 * (anything) ** NaN is NaN; 73 * NaN ** (anything except 0) is NaN; 74 * +(anything > 1) ** +INF is +INF; 75 * -(anything > 1) ** +INF is NaN; 76 * +-(anything > 1) ** -INF is +0; 77 * +-(anything < 1) ** +INF is +0; 78 * +(anything < 1) ** -INF is +INF; 79 * -(anything < 1) ** -INF is NaN; 80 * +-1 ** +-INF is NaN and signal INVALID; 81 * +0 ** +(anything except 0, NaN) is +0; 82 * -0 ** +(anything except 0, NaN, odd integer) is +0; 83 * +0 ** -(anything except 0, NaN) is +INF and signal DIV-BY-ZERO; 84 * -0 ** -(anything except 0, NaN, odd integer) is +INF with signal; 85 * -0 ** (odd integer) = -( +0 ** (odd integer) ); 86 * +INF ** +(anything except 0,NaN) is +INF; 87 * +INF ** -(anything except 0,NaN) is +0; 88 * -INF ** (odd integer) = -( +INF ** (odd integer) ); 89 * -INF ** (even integer) = ( +INF ** (even integer) ); 90 * -INF ** -(anything except integer,NaN) is NaN with signal; 91 * -(x=anything) ** (k=integer) is (-1)**k * (x ** k); 92 * -(anything except 0) ** (non-integer) is NaN with signal; 93 * 94 * Accuracy: 95 * pow(x,y) returns x**y nearly rounded. In particular, on a SUN, a VAX, 96 * and a Zilog Z8000, 97 * pow(integer,integer) 98 * always returns the correct integer provided it is representable. 99 * In a test run with 100,000 random arguments with 0 < x, y < 20.0 100 * on a VAX, the maximum observed error was 1.79 ulps (units in the 101 * last place). 102 * 103 * Constants : 104 * The hexadecimal values are the intended ones for the following constants. 105 * The decimal values may be used, provided that the compiler will convert 106 * from decimal to binary accurately enough to produce the hexadecimal values 107 * shown. 108 */ 109 110 #include <errno.h> 111 #include <math.h> 112 113 #include "mathimpl.h" 114 115 #if (defined(__vax__) || defined(tahoe)) 116 #define TRUNC(x) x = (double) (float) x 117 #define _IEEE 0 118 #else 119 #define _IEEE 1 120 #define endian (((*(int *) &one)) ? 1 : 0) 121 #define TRUNC(x) *(((int *) &x)+endian) &= 0xf8000000 122 #define infnan(x) 0.0 123 #endif /* __vax__ or tahoe */ 124 125 const static double zero=0.0, one=1.0, two=2.0, negone= -1.0; 126 127 static double pow_P __P((double, double)); 128 129 double pow(x,y) 130 double x,y; 131 { 132 double t; 133 if (y==zero) 134 return (one); 135 else if (y==one || (_IEEE && x != x)) 136 return (x); /* if x is NaN or y=1 */ 137 else if (_IEEE && y!=y) /* if y is NaN */ 138 return (y); 139 else if (!finite(y)) /* if y is INF */ 140 if ((t=fabs(x))==one) /* +-1 ** +-INF is NaN */ 141 return (y - y); 142 else if (t>one) 143 return ((y<0)? zero : ((x<zero)? y-y : y)); 144 else 145 return ((y>0)? zero : ((x<0)? y-y : -y)); 146 else if (y==two) 147 return (x*x); 148 else if (y==negone) 149 return (one/x); 150 /* x > 0, x == +0 */ 151 else if (copysign(one, x) == one) 152 return (pow_P(x, y)); 153 154 /* sign(x)= -1 */ 155 /* if y is an even integer */ 156 else if ( (t=drem(y,two)) == zero) 157 return (pow_P(-x, y)); 158 159 /* if y is an odd integer */ 160 else if (copysign(t,one) == one) 161 return (-pow_P(-x, y)); 162 163 /* Henceforth y is not an integer */ 164 else if (x==zero) /* x is -0 */ 165 return ((y>zero)? -x : one/(-x)); 166 else if (_IEEE) 167 return (zero/zero); 168 else 169 return (infnan(EDOM)); 170 } 171 /* kernel function for x >= 0 */ 172 static double 173 #ifdef _ANSI_SOURCE 174 pow_P(double x, double y) 175 #else 176 pow_P(x, y) double x, y; 177 #endif 178 { 179 struct Double s, t; 180 double huge = 1e300, tiny = 1e-300; 181 182 if (x == zero) { 183 if (y > zero) 184 return (zero); 185 else if (_IEEE) 186 return (huge*huge); 187 else 188 return (infnan(ERANGE)); 189 } 190 if (x == one) 191 return (one); 192 if (!finite(x)) { 193 if (y < zero) 194 return (zero); 195 else if (_IEEE) 196 return (huge*huge); 197 else 198 return (infnan(ERANGE)); 199 } 200 if (y >= 7e18) { /* infinity */ 201 if (x < 1) 202 return(tiny*tiny); 203 else if (_IEEE) 204 return (huge*huge); 205 else 206 return (infnan(ERANGE)); 207 } 208 209 /* Return exp(y*log(x)), using simulated extended */ 210 /* precision for the log and the multiply. */ 211 212 s = __log__D(x); 213 t.a = y; 214 TRUNC(t.a); 215 t.b = y - t.a; 216 t.b = s.b*y + t.b*s.a; 217 t.a *= s.a; 218 s.a = t.a + t.b; 219 s.b = (t.a - s.a) + t.b; 220 return (__exp__D(s.a, s.b)); 221 } 222