0dea5ff7ac05f5bacfd32b8f23d14d88ce72752e
[reactos.git] / reactos / lib / crt / stdlib / strtod.c
1 /* Copyright (C) 1996 DJ Delorie, see COPYING.DJ for details */
2 /* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
3
4 #include <precomp.h>
5
6 /*
7 * @implemented
8 */
9 double
10 strtod(const char *s, char **sret)
11 {
12 long double r; /* result */
13 int e; /* exponent */
14 long double d; /* scale */
15 int sign; /* +- 1.0 */
16 int esign;
17 int i;
18 int flags=0;
19
20 r = 0.0;
21 sign = 1;
22 e = 0;
23 esign = 1;
24
25 while ((*s == ' ') || (*s == '\t'))
26 s++;
27
28 if (*s == '+')
29 s++;
30 else if (*s == '-')
31 {
32 sign = -1;
33 s++;
34 }
35
36 while ((*s >= '0') && (*s <= '9'))
37 {
38 flags |= 1;
39 r *= 10.0;
40 r += *s - '0';
41 s++;
42 }
43
44 if (*s == '.')
45 {
46 d = 0.1L;
47 s++;
48 while ((*s >= '0') && (*s <= '9'))
49 {
50 flags |= 2;
51 r += d * (*s - '0');
52 s++;
53 d *= 0.1L;
54 }
55 }
56
57 if (flags == 0)
58 {
59 if (sret)
60 *sret = (char *)s;
61 return 0;
62 }
63
64 if ((*s == 'e') || (*s == 'E'))
65 {
66 s++;
67 if (*s == '+')
68 s++;
69 else if (*s == '-')
70 {
71 s++;
72 esign = -1;
73 }
74 if ((*s < '0') || (*s > '9'))
75 {
76 if (sret)
77 *sret = (char *)s;
78 return r;
79 }
80
81 while ((*s >= '0') && (*s <= '9'))
82 {
83 e *= 10;
84 e += *s - '0';
85 s++;
86 }
87 }
88
89 if (esign < 0)
90 for (i = 1; i <= e; i++)
91 r *= 0.1L;
92 else
93 for (i = 1; i <= e; i++)
94 r *= 10.0;
95
96 if (sret)
97 *sret = (char *)s;
98 return r * sign;
99 }