pow10.c (1438B)
1 /* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */ 2 #include "fmt.h" 3 #include "fmtdef.h" 4 #include "plan9.h" 5 6 #include <stdarg.h> 7 #include <string.h> 8 9 /* 10 * this table might overflow 127-bit exponent representations. 11 * in that case, truncate it after 1.0e38. 12 * it is important to get all one can from this 13 * routine since it is used in atof to scale numbers. 14 * the presumption is that C converts fp numbers better 15 * than multipication of lower powers of 10. 16 */ 17 18 static double tab[] = { 19 1.0e0, 20 1.0e1, 21 1.0e2, 22 1.0e3, 23 1.0e4, 24 1.0e5, 25 1.0e6, 26 1.0e7, 27 1.0e8, 28 1.0e9, 29 1.0e10, 30 1.0e11, 31 1.0e12, 32 1.0e13, 33 1.0e14, 34 1.0e15, 35 1.0e16, 36 1.0e17, 37 1.0e18, 38 1.0e19, 39 1.0e20, 40 1.0e21, 41 1.0e22, 42 1.0e23, 43 1.0e24, 44 1.0e25, 45 1.0e26, 46 1.0e27, 47 1.0e28, 48 1.0e29, 49 1.0e30, 50 1.0e31, 51 1.0e32, 52 1.0e33, 53 1.0e34, 54 1.0e35, 55 1.0e36, 56 1.0e37, 57 1.0e38, 58 1.0e39, 59 1.0e40, 60 1.0e41, 61 1.0e42, 62 1.0e43, 63 1.0e44, 64 1.0e45, 65 1.0e46, 66 1.0e47, 67 1.0e48, 68 1.0e49, 69 1.0e50, 70 1.0e51, 71 1.0e52, 72 1.0e53, 73 1.0e54, 74 1.0e55, 75 1.0e56, 76 1.0e57, 77 1.0e58, 78 1.0e59, 79 1.0e60, 80 1.0e61, 81 1.0e62, 82 1.0e63, 83 1.0e64, 84 1.0e65, 85 1.0e66, 86 1.0e67, 87 1.0e68, 88 1.0e69, 89 }; 90 91 double 92 __fmtpow10(int n) { 93 int m; 94 95 if (n < 0) { 96 n = -n; 97 if (n < (int) (sizeof(tab) / sizeof(tab[0]))) 98 return 1 / tab[n]; 99 m = n / 2; 100 return __fmtpow10(-m) * __fmtpow10(m - n); 101 } 102 if (n < (int) (sizeof(tab) / sizeof(tab[0]))) 103 return tab[n]; 104 m = n / 2; 105 return __fmtpow10(m) * __fmtpow10(n - m); 106 }