forked from Lucas-C/ministat
-
Notifications
You must be signed in to change notification settings - Fork 1
/
strtod.c
174 lines (158 loc) · 2.71 KB
/
strtod.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* vi:set ts=8 sts=4 sw=4: */
/*
* strtod implementation.
* author: Yasuhiro Matsumoto
* license: public domain
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <errno.h>
char*
skipwhite(q)
char *q;
{
char *p = q;
while (isspace(*p))
++p;
return p;
}
#define vim_isdigit(x) isdigit(x)
double
vim_strtod(str, end)
const char *str;
char **end;
{
double d = 0.0;
int sign;
int n = 0;
const char *p, *a;
a = p = str;
p = skipwhite(p);
/* decimal part */
sign = 1;
if (*p == '-')
{
sign = -1;
++p;
} else if (*p == '+')
++p;
if (vim_isdigit(*p))
{
d = (double)(*p++ - '0');
while (*p && vim_isdigit(*p))
{
d = d * 10.0 + (double)(*p - '0');
++p;
++n;
}
a = p;
} else if (*p != '.')
goto done;
d *= sign;
/* fraction part */
if (*p == '.')
{
double f = 0.0;
double base = 0.1;
++p;
if (vim_isdigit(*p))
{
while (*p && vim_isdigit(*p))
{
f += base * (*p - '0') ;
base /= 10.0;
++p;
++n;
}
}
d += f * sign;
a = p;
}
/* exponential part */
if ((*p == 'E') || (*p == 'e'))
{
int e = 0;
++p;
sign = 1;
if (*p == '-')
{
sign = -1;
++p;
} else if (*p == '+')
++p;
if (vim_isdigit(*p))
{
while (*p == '0')
++p;
if (*p == '\0') --p;
e = (int)(*p++ - '0');
while (*p && vim_isdigit(*p))
{
e = e * 10 + (int)(*p - '0');
++p;
}
e *= sign;
}
else if (!vim_isdigit(*(a-1)))
{
a = str;
goto done;
}
else if (*p == 0)
goto done;
if (d == 2.2250738585072011 && e == -308)
{
d = 0.0;
a = p;
errno = ERANGE;
goto done;
}
if (d == 2.2250738585072012 && e <= -308)
{
d *= 1.0e-308;
a = p;
goto done;
}
d *= pow(10.0, (double) e);
a = p;
}
else if (p > str && !vim_isdigit(*(p-1)))
{
a = str;
goto done;
}
done:
if (end)
*end = (char*)a;
return d;
}
void
test(char* str)
{
double d1, d2;
char *e1, *e2;
int x1, x2;
printf("CASE: %s\n", str);
errno = 0;
e1 = NULL;
d1 = vim_strtod(str, &e1);
x1 = errno;
errno = 0;
e2 = NULL;
d2 = strtod(str, &e2);
x2 = errno;
if (d1 != d2 || e1 != e2 || x1 != x2) {
printf(" ERR: %s, %s\n", str, strerror(errno));
printf(" E1 %f, %g, %s, %d\n", d1, d1, e1 ? e1 : "", x1);
printf(" E2 %f, %g, %s, %d\n", d2, d2, e2 ? e2 : "", x2);
if (d1 != d2) puts("different value");
if (e1 != e2) puts("different end position");
if (x1 != x2) puts("different errno");
} else {
printf(" SUCCESS [%f][%s]: %s\n", d1, e1 ? e1 : "", strerror(errno));
}
printf("\n");
}