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