|
|
1.1 root 1: /*
2: * C general utilities library internals.
3: * _pow10()
4: * Compute 10.0^n.
5: * Assumes -319 <= DBL_MIN_10_EXP and DBL_MAX_10_EXP <= 319,
6: * which is true for IEEE-format doubles.
7: * Modified for MS-DOS from the standard ANSI library source.
8: */
9:
10: /*
11: * There are lots of ways to do this, with varying accuracy, size, and speed.
12: * This version is nonrecursive and fast but uses somewhat bulky tables.
13: * It does the common cases -16 < exp < 16 by table lookup with no fp arithmetic.
14: * It does 16 <= exp <= DBL_MAX_10_EXP with one fp multiply.
15: * It does DBL_MIN_10_EXP <= exp <= -16 with one fp divide.
16: */
17:
18: #include <math.h>
19:
20: #if IEEE
21: #define DBL_MAX_10_EXP 308
22: #define DBL_MIN_10_EXP -307
23: #else
24: #define DBL_MAX_10_EXP 38
25: #define DBL_MIN_10_EXP -38
26: #endif
27:
28: static const double powtab0[] = {
29: 1e0, 1e-1, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6, 1e-7,
30: 1e-8, 1e-9, 1e-10, 1e-11, 1e-12, 1e-13, 1e-14, 1e-15
31: };
32:
33: static const double powtab1[] = {
34: 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7,
35: 1e8, 1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15
36: };
37:
38: static const double powtab2[] = {
39: #ifdef IEEE
40: 1e16, 1e32, 1e48, 1e64, 1e80, 1e96, 1e112, 1e128,
41: 1e144, 1e160, 1e176, 1e192, 1e208, 1e224, 1e240, 1e256,
42: 1e272, 1e288, 1e304
43: #else
44: 1e16, 1e32
45: #endif
46: };
47:
48: double
49: _pow10(exp) register int exp;
50: {
51: if (exp < 0) {
52: if ((exp = -exp) < 16)
53: return powtab0[exp];
54: else if (exp <= -DBL_MIN_10_EXP)
55: return powtab0[exp & 15] / powtab2[(exp >> 4) - 1];
56: else
57: return 0.0; /* exponent underflow */
58: }
59: else if (exp < 16)
60: return powtab1[exp];
61: else if (exp <= DBL_MAX_10_EXP)
62: return powtab1[exp & 15] * powtab2[(exp >> 4) - 1];
63: else
64: return HUGE_VAL; /* exponent overflow */
65: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.