/Python/strtod.c

http://unladen-swallow.googlecode.com/ · C · 156 lines · 89 code · 9 blank · 58 comment · 51 complexity · 848a535645960828b213abe4c3351a9c MD5 · raw file

  1. #include "pyconfig.h"
  2. /* comp.sources.misc strtod(), as posted in comp.lang.tcl,
  3. with bugfix for "123000.0" and acceptance of space after 'e' sign nuked.
  4. ************************************************************
  5. * YOU MUST EDIT THE MACHINE-DEPENDENT DEFINITIONS BELOW!!! *
  6. ************************************************************
  7. */
  8. /* File : stdtod.c (Modified version of str2dbl.c)
  9. Author : Richard A. O'Keefe @ Quintus Computer Systems, Inc.
  10. Updated: Tuesday August 2nd, 1988
  11. Defines: double strtod (char *str, char**ptr)
  12. */
  13. /* This is an implementation of the strtod() function described in the
  14. System V manuals, with a different name to avoid linker problems.
  15. All that str2dbl() does itself is check that the argument is well-formed
  16. and is in range. It leaves the work of conversion to atof(), which is
  17. assumed to exist and deliver correct results (if they can be represented).
  18. There are two reasons why this should be provided to the net:
  19. (a) some UNIX systems do not yet have strtod(), or do not have it
  20. available in the BSD "universe" (but they do have atof()).
  21. (b) some of the UNIX systems that *do* have it get it wrong.
  22. (some crash with large arguments, some assign the wrong *ptr value).
  23. There is a reason why *we* are providing it: we need a correct version
  24. of strtod(), and if we give this one away maybe someone will look for
  25. mistakes in it and fix them for us (:-).
  26. */
  27. /* The following constants are machine-specific. MD{MIN,MAX}EXPT are
  28. integers and MD{MIN,MAX}FRAC are strings such that
  29. 0.${MDMAXFRAC}e${MDMAXEXPT} is the largest representable double,
  30. 0.${MDMINFRAC}e${MDMINEXPT} is the smallest representable +ve double
  31. MD{MIN,MAX}FRAC must not have any trailing zeros.
  32. The values here are for IEEE-754 64-bit floats.
  33. It is not perfectly clear to me whether an IEEE infinity should be
  34. returned for overflow, nor what a portable way of writing one is,
  35. so HUGE is just 0.MAXFRAC*10**MAXEXPT (this seems still to be the
  36. UNIX convention).
  37. I do know about <values.h>, but the whole point of this file is that
  38. we can't always trust that stuff to be there or to be correct.
  39. */
  40. static int MDMINEXPT = -323;
  41. static char MDMINFRAC[] = "494065645841246544";
  42. static double ZERO = 0.0;
  43. static int MDMAXEXPT = 309;
  44. static char MDMAXFRAC[] = "17976931348623157";
  45. static double HUGE = 1.7976931348623157e308;
  46. extern double atof(const char *); /* Only called when result known to be ok */
  47. #ifdef HAVE_ERRNO_H
  48. #include <errno.h>
  49. #endif
  50. extern int errno;
  51. double strtod(char *str, char **ptr)
  52. {
  53. int sign, scale, dotseen;
  54. int esign, expt;
  55. char *save;
  56. register char *sp, *dp;
  57. register int c;
  58. char *buforg, *buflim;
  59. char buffer[64]; /* 45-digit significant + */
  60. /* 13-digit exponent */
  61. sp = str;
  62. while (*sp == ' ') sp++;
  63. sign = 1;
  64. if (*sp == '-') sign -= 2, sp++;
  65. dotseen = 0, scale = 0;
  66. dp = buffer;
  67. *dp++ = '0'; *dp++ = '.';
  68. buforg = dp, buflim = buffer+48;
  69. for (save = sp; c = *sp; sp++)
  70. if (c == '.') {
  71. if (dotseen) break;
  72. dotseen++;
  73. } else
  74. if ((unsigned)(c-'0') > (unsigned)('9'-'0')) {
  75. break;
  76. } else
  77. if (c == '0') {
  78. if (dp != buforg) {
  79. /* This is not the first digit, so we want to keep it */
  80. if (dp < buflim) *dp++ = c;
  81. if (!dotseen) scale++;
  82. } else {
  83. /* No non-zero digits seen yet */
  84. /* If a . has been seen, scale must be adjusted */
  85. if (dotseen) scale--;
  86. }
  87. } else {
  88. /* This is a nonzero digit, so we want to keep it */
  89. if (dp < buflim) *dp++ = c;
  90. /* If it precedes a ., scale must be adjusted */
  91. if (!dotseen) scale++;
  92. }
  93. if (sp == save) {
  94. if (ptr) *ptr = str;
  95. errno = EDOM; /* what should this be? */
  96. return ZERO;
  97. }
  98. while (dp > buforg && dp[-1] == '0') --dp;
  99. if (dp == buforg) *dp++ = '0';
  100. *dp = '\0';
  101. /* Now the contents of buffer are
  102. +--+--------+-+--------+
  103. |0.|fraction|\|leftover|
  104. +--+--------+-+--------+
  105. ^dp points here
  106. where fraction begins with 0 iff it is "0", and has at most
  107. 45 digits in it, and leftover is at least 16 characters.
  108. */
  109. save = sp, expt = 0, esign = 1;
  110. do {
  111. c = *sp++;
  112. if (c != 'e' && c != 'E') break;
  113. c = *sp++;
  114. if (c == '-') esign -= 2, c = *sp++; else
  115. if (c == '+' /* || c == ' ' */ ) c = *sp++;
  116. if ((unsigned)(c-'0') > (unsigned)('9'-'0')) break;
  117. while (c == '0') c = *sp++;
  118. for (; (unsigned)(c-'0') <= (unsigned)('9'-'0'); c = *sp++)
  119. expt = expt*10 + c-'0';
  120. if (esign < 0) expt = -expt;
  121. save = sp-1;
  122. } while (0);
  123. if (ptr) *ptr = save;
  124. expt += scale;
  125. /* Now the number is sign*0.fraction*10**expt */
  126. errno = ERANGE;
  127. if (expt > MDMAXEXPT) {
  128. return HUGE*sign;
  129. } else
  130. if (expt == MDMAXEXPT) {
  131. if (strcmp(buforg, MDMAXFRAC) > 0) return HUGE*sign;
  132. } else
  133. if (expt < MDMINEXPT) {
  134. return ZERO*sign;
  135. } else
  136. if (expt == MDMINEXPT) {
  137. if (strcmp(buforg, MDMINFRAC) < 0) return ZERO*sign;
  138. }
  139. /* We have now established that the number can be */
  140. /* represented without overflow or underflow */
  141. (void) sprintf(dp, "E%d", expt);
  142. errno = 0;
  143. return atof(buffer)*sign;
  144. }