2 Copyright © 1995-2011, The AROS Development Team. All rights reserved.
5 ANSI C function strtod().
14 /*****************************************************************************
27 Convert a string of digits into a double.
30 str - The string which should be converted. Leading
31 whitespace are ignored. The number may be prefixed
32 by a '+' or '-'. An 'e' or 'E' introduces the exponent.
33 Komma is only allowed before exponent.
34 endptr - If this is non-NULL, then the address of the first
35 character after the number in the string is stored
39 The value of the string. The first character after the number
40 is returned in *endptr, if endptr is non-NULL. If no digits can
41 be converted, *endptr contains str (if non-NULL) and 0 is
49 NAN is not handled at the moment
52 atof(), atoi(), atol(), strtol(), strtoul()
56 ******************************************************************************/
58 /* Unit tests available in : tests/clib/strtod.c */
59 /* FIXME: implement NAN handling */
60 double val
= 0, precision
;
65 /* assign initial value in case nothing will be found */
67 *endptr
= (char *)str
;
69 /* skip all leading spaces */
70 while (isspace (*str
))
73 /* start with scanning the floting point number */
76 /* Is there a sign? */
77 if (*str
== '+' || *str
== '-')
80 /* scan numbers before the dot */
84 val
= val
* 10 + (*str
- '0');
88 /* see if there is the dot and there were digits before it or there is
89 at least one digit after it */
90 if ((*str
== '.') && ((digits
> 0) || (isdigit(*(str
+ 1)))))
93 /* scan the numbers behind the dot */
95 while (isdigit (*str
))
98 val
+= ((*str
- '0') * precision
) ;
100 precision
= precision
* 0.1;
104 /* look for a sequence like "E+10" or "e-22" if there were any digits up to now */
105 if ((digits
> 0) && (tolower(*str
) == 'e'))
110 if (*str
== '+' || *str
== '-')
113 while (isdigit (*str
))
116 exp
= exp
* 10 + (*str
- '0');
125 /* there were no digits after 'e' - rollback pointer */
126 str
--; if (c2
!= 0) str
--;
129 val
*= pow (10, exp
);
135 if ((digits
== 0) && (c
!= 0))
137 /* there were no digits but there was sign - rollback pointer */
142 /* something was found, assign the pointer value */
143 if (endptr
&& digits
> 0)
144 *endptr
= (char *)str
;
151 void strtod (const char * str
,char ** endptr
)
156 #endif /* AROS_NOFPU */