pow10.c - 9base - revived minimalist port of Plan 9 userland to Unix
 (HTM) git clone git://git.suckless.org/9base
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       pow10.c (1301B)
       ---
            1 /* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */
            2 #include <stdarg.h>
            3 #include <string.h>
            4 #include "plan9.h"
            5 #include "fmt.h"
            6 #include "fmtdef.h"
            7 
            8 /*
            9  * this table might overflow 127-bit exponent representations.
           10  * in that case, truncate it after 1.0e38.
           11  * it is important to get all one can from this
           12  * routine since it is used in atof to scale numbers.
           13  * the presumption is that C converts fp numbers better
           14  * than multipication of lower powers of 10.
           15  */
           16 
           17 static
           18 double        tab[] =
           19 {
           20         1.0e0, 1.0e1, 1.0e2, 1.0e3, 1.0e4, 1.0e5, 1.0e6, 1.0e7, 1.0e8, 1.0e9,
           21         1.0e10,1.0e11,1.0e12,1.0e13,1.0e14,1.0e15,1.0e16,1.0e17,1.0e18,1.0e19,
           22         1.0e20,1.0e21,1.0e22,1.0e23,1.0e24,1.0e25,1.0e26,1.0e27,1.0e28,1.0e29,
           23         1.0e30,1.0e31,1.0e32,1.0e33,1.0e34,1.0e35,1.0e36,1.0e37,1.0e38,1.0e39,
           24         1.0e40,1.0e41,1.0e42,1.0e43,1.0e44,1.0e45,1.0e46,1.0e47,1.0e48,1.0e49,
           25         1.0e50,1.0e51,1.0e52,1.0e53,1.0e54,1.0e55,1.0e56,1.0e57,1.0e58,1.0e59,
           26         1.0e60,1.0e61,1.0e62,1.0e63,1.0e64,1.0e65,1.0e66,1.0e67,1.0e68,1.0e69,
           27 };
           28 
           29 double
           30 __fmtpow10(int n)
           31 {
           32         int m;
           33 
           34         if(n < 0) {
           35                 n = -n;
           36                 if(n < (int)(sizeof(tab)/sizeof(tab[0])))
           37                         return 1/tab[n];
           38                 m = n/2;
           39                 return __fmtpow10(-m) * __fmtpow10(m-n);
           40         }
           41         if(n < (int)(sizeof(tab)/sizeof(tab[0])))
           42                 return tab[n];
           43         m = n/2;
           44         return __fmtpow10(m) * __fmtpow10(n-m);
           45 }