annotate Python/atof.c @ 52949:31582485fbfc

Revert removal of atof.c and strtod.c in r71341. We're not quite ready for this yet---this removal should happen as part of the py3k-short-float-repr merge.
author Mark Dickinson <dickinsm@gmail.com>
date Tue, 07 Apr 2009 11:53:52 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
52949
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2 /* Just in case you haven't got an atof() around...
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
3 This one doesn't check for bad syntax or overflow,
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
4 and is slow and inaccurate.
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
5 But it's good enough for the occasional string literal... */
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
6
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
7 #include "pyconfig.h"
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
8
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
9 #include <ctype.h>
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
10
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
11 double atof(char *s)
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
12 {
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
13 double a = 0.0;
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
14 int e = 0;
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
15 int c;
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
16 while ((c = *s++) != '\0' && isdigit(c)) {
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
17 a = a*10.0 + (c - '0');
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
18 }
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
19 if (c == '.') {
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
20 while ((c = *s++) != '\0' && isdigit(c)) {
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
21 a = a*10.0 + (c - '0');
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
22 e = e-1;
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
23 }
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
24 }
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
25 if (c == 'e' || c == 'E') {
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
26 int sign = 1;
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
27 int i = 0;
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
28 c = *s++;
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
29 if (c == '+')
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
30 c = *s++;
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
31 else if (c == '-') {
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
32 c = *s++;
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
33 sign = -1;
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
34 }
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
35 while (isdigit(c)) {
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
36 i = i*10 + (c - '0');
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
37 c = *s++;
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
38 }
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
39 e += i*sign;
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
40 }
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
41 while (e > 0) {
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
42 a *= 10.0;
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
43 e--;
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
44 }
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
45 while (e < 0) {
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
46 a *= 0.1;
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
47 e++;
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
48 }
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
49 return a;
31582485fbfc Revert removal of atof.c and strtod.c in r71341. We're not quite
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
50 }