blob: a0991d5c687e95375d62167059d956315c6c2b78 [file] [log] [blame]
Guido van Rossum2b7e04a1995-02-19 15:54:36 +00001#include "config.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
Guido van Rossum23d5cde1992-01-14 18:42:23 +000017/* This is an implementation of the strtod() function described in the
18 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
25 available in the BSD "universe" (but they do have atof()).
26 (b) some of the UNIX systems that *do* have it get it wrong.
27 (some crash with large arguments, some assign the wrong *ptr value).
28 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*/
32
33/* The following constants are machine-specific. MD{MIN,MAX}EXPT are
34 integers and MD{MIN,MAX}FRAC are strings such that
35 0.${MDMAXFRAC}e${MDMAXEXPT} is the largest representable double,
36 0.${MDMINFRAC}e${MDMINEXPT} is the smallest representable +ve double
37 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*/
47static int MDMINEXPT = {-323};
48static char MDMINFRAC[] = "494065645841246544";
49static double ZERO = 0.0;
50
51static int MDMAXEXPT = { 309};
Guido van Rossumdcc6ef21992-03-27 17:30:32 +000052static char MDMAXFRAC[] = "17976931348623157";
53static double HUGE = 1.7976931348623157e308;
Guido van Rossum23d5cde1992-01-14 18:42:23 +000054
55extern double atof(); /* Only called when result known to be ok */
56
Guido van Rossum2571cc81999-04-07 16:07:23 +000057#ifndef DONT_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
Guido van Rossum23d5cde1992-01-14 18:42:23 +000060extern int errno;
61
62double strtod(str, ptr)
63 char *str;
64 char **ptr;
65 {
66 int sign, scale, dotseen;
67 int esign, expt;
68 char *save;
69 register char *sp, *dp;
70 register int c;
71 char *buforg, *buflim;
72 char buffer[64]; /* 45-digit significand + */
73 /* 13-digit exponent */
74 sp = str;
75 while (*sp == ' ') sp++;
76 sign = 1;
77 if (*sp == '-') sign -= 2, sp++;
78 dotseen = 0, scale = 0;
79 dp = buffer;
80 *dp++ = '0'; *dp++ = '.';
81 buforg = dp, buflim = buffer+48;
82 for (save = sp; c = *sp; sp++)
83 if (c == '.') {
84 if (dotseen) break;
85 dotseen++;
86 } else
87 if ((unsigned)(c-'0') > (unsigned)('9'-'0')) {
88 break;
89 } else
90 if (c == '0') {
91 if (dp != buforg) {
92 /* This is not the first digit, so we want to keep it */
93 if (dp < buflim) *dp++ = c;
94 if (!dotseen) scale++;
95 } else {
96 /* No non-zero digits seen yet */
97 /* If a . has been seen, scale must be adjusted */
98 if (dotseen) scale--;
99 }
100 } else {
101 /* This is a nonzero digit, so we want to keep it */
102 if (dp < buflim) *dp++ = c;
103 /* If it precedes a ., scale must be adjusted */
104 if (!dotseen) scale++;
105 }
106 if (sp == save) {
107 if (ptr) *ptr = str;
108 errno = EDOM; /* what should this be? */
109 return ZERO;
110 }
Guido van Rossum5afc7471991-12-31 13:15:19 +0000111
Guido van Rossum23d5cde1992-01-14 18:42:23 +0000112 while (dp > buforg && dp[-1] == '0') --dp;
113 if (dp == buforg) *dp++ = '0';
114 *dp = '\0';
115 /* Now the contents of buffer are
116 +--+--------+-+--------+
117 |0.|fraction|\|leftover|
118 +--+--------+-+--------+
119 ^dp points here
120 where fraction begins with 0 iff it is "0", and has at most
121 45 digits in it, and leftover is at least 16 characters.
122 */
123 save = sp, expt = 0, esign = 1;
124 do {
125 c = *sp++;
126 if (c != 'e' && c != 'E') break;
127 c = *sp++;
128 if (c == '-') esign -= 2, c = *sp++; else
129 if (c == '+' /* || c == ' ' */ ) c = *sp++;
130 if ((unsigned)(c-'0') > (unsigned)('9'-'0')) break;
131 while (c == '0') c = *sp++;
132 for (; (unsigned)(c-'0') <= (unsigned)('9'-'0'); c = *sp++)
133 expt = expt*10 + c-'0';
134 if (esign < 0) expt = -expt;
135 save = sp-1;
136 } while (0);
137 if (ptr) *ptr = save;
138 expt += scale;
139 /* Now the number is sign*0.fraction*10**expt */
140 errno = ERANGE;
141 if (expt > MDMAXEXPT) {
142 return HUGE*sign;
143 } else
144 if (expt == MDMAXEXPT) {
145 if (strcmp(buforg, MDMAXFRAC) > 0) return HUGE*sign;
146 } else
147 if (expt < MDMINEXPT) {
148 return ZERO*sign;
149 } else
150 if (expt == MDMINEXPT) {
151 if (strcmp(buforg, MDMINFRAC) < 0) return ZERO*sign;
152 }
153 /* We have now established that the number can be */
154 /* represented without overflow or underflow */
155 (void) sprintf(dp, "E%d", expt);
156 errno = 0;
157 return atof(buffer)*sign;
158 }