1 /* $NetBSD: n_pow.c,v 1.6 2002/06/15 00:10:18 matt Exp $ */
3 * Copyright (c) 1985, 1993
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 static char sccsid
[] = "@(#)pow.c 8.1 (Berkeley) 6/4/93";
39 * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
40 * CODED IN C BY K.C. NG, 1/8/85;
41 * REVISED BY K.C. NG on 7/10/85.
42 * KERNEL pow_P() REPLACED BY P. McILROY 7/22/92.
43 * Required system supported functions:
50 * Required kernel functions:
51 * exp__D(a,c) exp(a + c) for |a| << |c|
52 * struct d_double dlog(x) r.a + r.b, |r.b| < |r.a|
55 * 1. Compute and return log(x) in three pieces:
56 * log(x) = n*ln2 + hi + lo,
57 * where n is an integer.
58 * 2. Perform y*log(x) by simulating muti-precision arithmetic and
59 * return the answer in three pieces:
60 * y*log(x) = m*ln2 + hi + lo,
61 * where m is an integer.
62 * 3. Return x**y = exp(y*log(x))
63 * = 2^m * ( exp(hi+lo) ).
66 * (anything) ** 0 is 1 ;
67 * (anything) ** 1 is itself;
68 * (anything) ** NaN is NaN;
69 * NaN ** (anything except 0) is NaN;
70 * +(anything > 1) ** +INF is +INF;
71 * -(anything > 1) ** +INF is NaN;
72 * +-(anything > 1) ** -INF is +0;
73 * +-(anything < 1) ** +INF is +0;
74 * +(anything < 1) ** -INF is +INF;
75 * -(anything < 1) ** -INF is NaN;
76 * +-1 ** +-INF is NaN and signal INVALID;
77 * +0 ** +(anything except 0, NaN) is +0;
78 * -0 ** +(anything except 0, NaN, odd integer) is +0;
79 * +0 ** -(anything except 0, NaN) is +INF and signal DIV-BY-ZERO;
80 * -0 ** -(anything except 0, NaN, odd integer) is +INF with signal;
81 * -0 ** (odd integer) = -( +0 ** (odd integer) );
82 * +INF ** +(anything except 0,NaN) is +INF;
83 * +INF ** -(anything except 0,NaN) is +0;
84 * -INF ** (odd integer) = -( +INF ** (odd integer) );
85 * -INF ** (even integer) = ( +INF ** (even integer) );
86 * -INF ** -(anything except integer,NaN) is NaN with signal;
87 * -(x=anything) ** (k=integer) is (-1)**k * (x ** k);
88 * -(anything except 0) ** (non-integer) is NaN with signal;
91 * pow(x,y) returns x**y nearly rounded. In particular, on a SUN, a VAX,
93 * pow(integer,integer)
94 * always returns the correct integer provided it is representable.
95 * In a test run with 100,000 random arguments with 0 < x, y < 20.0
96 * on a VAX, the maximum observed error was 1.79 ulps (units in the
100 * The hexadecimal values are the intended ones for the following constants.
101 * The decimal values may be used, provided that the compiler will convert
102 * from decimal to binary accurately enough to produce the hexadecimal values
109 #include "mathimpl.h"
111 #if (defined(__vax__) || defined(tahoe))
112 #define TRUNC(x) x = (double) (float) x
116 #define endian (((*(int *) &one)) ? 1 : 0)
117 #define TRUNC(x) *(((int *) &x)+endian) &= 0xf8000000
118 #define infnan(x) 0.0
119 #endif /* __vax__ or tahoe */
121 static const double zero
=0.0, one
=1.0, two
=2.0, negone
= -1.0;
123 static double pow_P (double, double);
126 powf(float x
, float y
)
128 return pow((double) x
, (double) (y
));
132 pow(double x
, double y
)
137 else if (y
==one
|| (_IEEE
&& x
!= x
))
138 return (x
); /* if x is NaN or y=1 */
139 else if (_IEEE
&& y
!=y
) /* if y is NaN */
141 else if (!finite(y
)) /* if y is INF */
142 if ((t
=fabs(x
))==one
) /* +-1 ** +-INF is NaN */
145 return ((y
<0)? zero
: ((x
<zero
)? y
-y
: y
));
147 return ((y
>0)? zero
: ((x
<0)? y
-y
: -y
));
153 else if (copysign(one
, x
) == one
)
154 return (pow_P(x
, y
));
157 /* if y is an even integer */
158 else if ( (t
=drem(y
,two
)) == zero
)
159 return (pow_P(-x
, y
));
161 /* if y is an odd integer */
162 else if (copysign(t
,one
) == one
)
163 return (-pow_P(-x
, y
));
165 /* Henceforth y is not an integer */
166 else if (x
==zero
) /* x is -0 */
167 return ((y
>zero
)? -x
: one
/(-x
));
171 return (infnan(EDOM
));
174 /* kernel function for x >= 0 */
176 pow_P(double x
, double y
)
179 double huge
= 1e300
, tiny
= 1e-300;
187 return (infnan(ERANGE
));
197 return (infnan(ERANGE
));
199 if (y
>= 7e18
) { /* infinity */
205 return (infnan(ERANGE
));
208 /* Return exp(y*log(x)), using simulated extended */
209 /* precision for the log and the multiply. */
215 t
.b
= s
.b
*y
+ t
.b
*s
.a
;
218 s
.b
= (t
.a
- s
.a
) + t
.b
;
219 return (__exp__D(s
.a
, s
.b
));