2 Copyright © 1995-2012, The AROS Development Team. All rights reserved.
13 /*****************************************************************************
26 Convert a string of digits into a double.
29 str - The string which should be converted. Leading
30 whitespace are ignored. The number may be prefixed
31 by a '+' or '-'. An 'e' or 'E' introduces the exponent.
32 Komma is only allowed before exponent.
33 endptr - If this is non-NULL, then the address of the first
34 character after the number in the string is stored
38 The value of the string. The first character after the number
39 is returned in *endptr, if endptr is non-NULL. If no digits can
40 be converted, *endptr contains str (if non-NULL) and 0 is
48 NAN is not handled at the moment
51 atof(), atoi(), atol(), strtol(), strtoul()
55 ******************************************************************************/
57 /* Unit tests available in : tests/clib/strtod.c */
58 /* FIXME: implement NAN handling */
59 double val
= 0, precision
;
64 /* assign initial value in case nothing will be found */
66 *endptr
= (char *)str
;
68 /* skip all leading spaces */
69 while (isspace (*str
))
72 /* start with scanning the floting point number */
75 /* Is there a sign? */
76 if (*str
== '+' || *str
== '-')
79 /* scan numbers before the dot */
83 val
= val
* 10 + (*str
- '0');
87 /* see if there is the dot and there were digits before it or there is
88 at least one digit after it */
89 if ((*str
== '.') && ((digits
> 0) || (isdigit(*(str
+ 1)))))
92 /* scan the numbers behind the dot */
94 while (isdigit (*str
))
97 val
+= ((*str
- '0') * precision
) ;
99 precision
= precision
* 0.1;
103 /* look for a sequence like "E+10" or "e-22" if there were any digits up to now */
104 if ((digits
> 0) && (tolower(*str
) == 'e'))
109 if (*str
== '+' || *str
== '-')
112 while (isdigit (*str
))
115 exp
= exp
* 10 + (*str
- '0');
124 /* there were no digits after 'e' - rollback pointer */
125 str
--; if (c2
!= 0) str
--;
128 val
*= pow (10, exp
);
134 if ((digits
== 0) && (c
!= 0))
136 /* there were no digits but there was sign - rollback pointer */
141 /* something was found, assign the pointer value */
142 if (endptr
&& digits
> 0)
143 *endptr
= (char *)str
;
150 void strtod (const char * str
,char ** endptr
)
155 #endif /* AROS_NOFPU */