Expontation function.
Method: Let x = 2 * (1+f)
1. Compute and return log2(x) in two pieces:
log2(x) = w1 + w2,
where w1 has 53-24 = 29 bit trailing zeros.
2. Perform y*log2(x) = n+y' by simulating muti-precision
arithmetic, where |y'|<=0.5.
3. Return x**y = 2**n*exp(y'*log2)
Special cases:
1. (anything) ** 0 is 1
2. (anything) ** 1 is itself
3. (anything) ** NAN is NAN
4. NAN ** (anything except 0) is NAN
5. +-(|x| > 1) ** +INF is +INF
6. +-(|x| > 1) ** -INF is +0
7. +-(|x| < 1) ** +INF is +0
8. +-(|x| < 1) ** -INF is +INF
9. +-1 ** +-INF is NAN
10. +0 ** (+anything except 0, NAN) is +0
11. -0 ** (+anything except 0, NAN, odd integer) is +0
12. +0 ** (-anything except 0, NAN) is +INF
13. -0 ** (-anything except 0, NAN, odd integer) is +INF
14. -0 ** (odd integer) = -( +0 ** (odd integer) )
15. +INF ** (+anything except 0,NAN) is +INF
16. +INF ** (-anything except 0,NAN) is +0
17. -INF ** (anything) = -0 ** (-anything)
18. (-anything) ** (integer) is (-1)**(integer)*(+anything**integer)
19. (-anything except 0 and inf) ** (non-integer) is NAN
Accuracy:
pow(x,y) returns x**y nearly rounded. In particular
pow(integer,integer)
always returns the correct integer provided it is
representable.
Constants :
The hexadecimal values are the intended ones for the following
constants. The decimal values may be used, provided that the
compiler will convert from decimal to binary accurately enough
to produce the hexadecimal values shown.
Definition at line 138 of file pow.c.
References bp, cp, cp_h, cp_l, dp_h, dp_l, fabs(), huge, ivln2, ivln2_h, ivln2_l, L1, L2, L3, L4, L5, L6, lg2, lg2_h, lg2_l, one, ovt, P1, P2, P3, P4, P5, scalbn(), sqrt(), tiny, two, two53, and zero.
Referenced by cpow(), PositionalNumeralSystem::GetText(), PositionalNumeralSystem::Parse(), and RealNumber::Raise().
140 double z, ax, z_h, z_l, p_h, p_l;
141 double y1, t1, t2, r, s, t, u, v, w;
142 int32_t i, j, k, yisint, n;
143 int32_t hx, hy, ix, iy;
148 ix = hx & 0x7fffffff;
149 iy = hy & 0x7fffffff;
156 if (ix > 0x7ff00000 || ((ix == 0x7ff00000) && (lx != 0)) ||
157 iy > 0x7ff00000 || ((iy == 0x7ff00000) && (ly != 0)))
168 if (iy >= 0x43400000)
170 else if (iy >= 0x3ff00000)
172 k = (iy >> 20) - 0x3ff;
176 if ((uint32_t)(j << (52 - k)) == ly)
177 yisint = 2 - (j & 1);
182 if ((j << (20 - k)) == iy)
183 yisint = 2 - (j & 1);
191 if (iy == 0x7ff00000)
193 if (((ix - 0x3ff00000) | lx) == 0)
195 else if (ix >= 0x3ff00000)
196 return (hy >= 0) ? y :
zero;
198 return (hy < 0) ? -y :
zero;
200 if (iy == 0x3ff00000)
207 if (hy == 0x40000000)
209 if (hy == 0x3fe00000)
220 if (ix == 0x7ff00000 || ix == 0 || ix == 0x3ff00000)
227 if (((ix - 0x3ff00000) | yisint) == 0)
229 z = (z - z) / (z - z);
231 else if (yisint == 1)
241 if ((n | yisint) == 0)
242 return (x - x) / (x - x);
245 if ((n | (yisint - 1)) == 0)
253 if (ix <= 0x3fefffff)
255 if (ix >= 0x3ff00000)
266 w = (t * t) * (0.5 - t * (0.3333333333333333333333 - t * 0.25));
275 double ss, s2, s_h, s_l, t_h, t_l;
284 n += ((ix) >> 20) - 0x3ff;
290 else if (j < 0xBB67A)
302 v = one / (ax + bp[k]);
308 SET_HIGH_WORD(t_h, ((ix >> 1) | 0x20000000) + 0x00080000 + (k << 18));
309 t_l = ax - (t_h - bp[k]);
310 s_l = v * ((u - s_h * t_h) - s_h * t_l);
313 r = s2 * s2 * (
L1 + s2 * (
L2 + s2 * (
L3 + s2 * (
L4 + s2 * (
L5 + s2 *
L6)))));
314 r += s_l * (s_h + ss);
318 t_l = r - ((t_h - 3.0) - s2);
321 v = s_l * t_h + t_l * ss;
330 t1 = (((z_h + z_l) +
dp_h[k]) + t);
332 t2 = z_l - (((t1 - t) -
dp_h[k]) - z_h);
338 p_l = (y - y1) * t1 + y * t2;
344 if (((j - 0x40900000) | i) != 0)
348 if (p_l +
ovt > z - p_h)
349 return s * huge *
huge;
352 else if ((j & 0x7fffffff) >= 0x4090cc00)
354 if (((j - 0xc090cc00) | i) != 0)
355 return s * tiny *
tiny;
359 return s * tiny *
tiny;
366 k = (i >> 20) - 0x3ff;
370 n = j + (0x00100000 >> (k + 1));
371 k = ((n & 0x7fffffff) >> 20) - 0x3ff;
374 n = ((n & 0x000fffff) | 0x00100000) >> (20 - k);
382 v = (p_l - (t - p_h)) *
lg2 + t *
lg2_l;
386 t1 = z - t * (
P1 + t * (
P2 + t * (
P3 + t * (
P4 + t *
P5))));
387 r = (z * t1) / (t1 -
two) - (w + z * w);
#define GET_HIGH_WORD(i, d)
Get the more significant 32 bit int from a double.
static const double lg2_h
static const double dp_h[]
static const double ivln2_h
static const double two53
#define EXTRACT_WORDS(ix0, ix1, d)
Get two 32 bit ints from a double.
static const double ivln2_l
static const double ivln2
double scalbn(double x, int n)
static const double dp_l[]
static const double lg2_l
#define SET_LOW_WORD(d, v)
Set the less significant 32 bits of a double from an int.
double sqrt(double x)
Square root function.
#define SET_HIGH_WORD(d, v)
Set the more significant 32 bits of a double from an int.
double fabs(double x)
Returns the absolute value of x.