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