pow10.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * The authors of this software are Rob Pike and Ken Thompson.
  3. * Copyright (c) 2002 by Lucent Technologies.
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose without fee is hereby granted, provided that this entire notice
  6. * is included in all copies of any software which is or includes a copy
  7. * or modification of this software and in all copies of the supporting
  8. * documentation for such software.
  9. * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
  10. * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE ANY
  11. * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
  12. * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
  13. */
  14. #include <stdarg.h>
  15. #include <string.h>
  16. #include "utf.h"
  17. #include "fmt.h"
  18. #include "fmtdef.h"
  19. /*
  20. * this table might overflow 127-bit exponent representations.
  21. * in that case, truncate it after 1.0e38.
  22. * it is important to get all one can from this
  23. * routine since it is used in atof to scale numbers.
  24. * the presumption is that C converts fp numbers better
  25. * than multipication of lower powers of 10.
  26. */
  27. static
  28. double tab[] =
  29. {
  30. 1.0e0, 1.0e1, 1.0e2, 1.0e3, 1.0e4, 1.0e5, 1.0e6, 1.0e7, 1.0e8, 1.0e9,
  31. 1.0e10,1.0e11,1.0e12,1.0e13,1.0e14,1.0e15,1.0e16,1.0e17,1.0e18,1.0e19,
  32. 1.0e20,1.0e21,1.0e22,1.0e23,1.0e24,1.0e25,1.0e26,1.0e27,1.0e28,1.0e29,
  33. 1.0e30,1.0e31,1.0e32,1.0e33,1.0e34,1.0e35,1.0e36,1.0e37,1.0e38,1.0e39,
  34. 1.0e40,1.0e41,1.0e42,1.0e43,1.0e44,1.0e45,1.0e46,1.0e47,1.0e48,1.0e49,
  35. 1.0e50,1.0e51,1.0e52,1.0e53,1.0e54,1.0e55,1.0e56,1.0e57,1.0e58,1.0e59,
  36. 1.0e60,1.0e61,1.0e62,1.0e63,1.0e64,1.0e65,1.0e66,1.0e67,1.0e68,1.0e69,
  37. };
  38. double
  39. __fmtpow10(int n)
  40. {
  41. int m;
  42. if(n < 0) {
  43. n = -n;
  44. if(n < (int)(sizeof(tab)/sizeof(tab[0])))
  45. return 1/tab[n];
  46. m = n/2;
  47. return __fmtpow10(-m) * __fmtpow10(m-n);
  48. }
  49. if(n < (int)(sizeof(tab)/sizeof(tab[0])))
  50. return tab[n];
  51. m = n/2;
  52. return __fmtpow10(m) * __fmtpow10(n-m);
  53. }