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