2 Copyright © 1995-2001, 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 /* skip all leading spaces */
66 while (isspace (*str
))
69 /* start with scanning the floting point number */
72 /* Is there a sign? */
73 if (*str
== '+' || *str
== '-')
76 /* scan numbers before the dot */
80 val
= val
* 10 + (*str
- '0');
84 /* see if there is the dot and there were digits before it or there is
85 at least one digit after it */
86 if ((*str
== '.') && ((digits
> 0) || (isdigit(*(str
+ 1)))))
89 /* scan the numbers behind the dot */
91 while (isdigit (*str
))
94 val
+= ((*str
- '0') * precision
) ;
96 precision
= precision
* 0.1;
100 /* look for a sequence like "E+10" or "e-22" if there were any digits up to now */
101 if ((digits
> 0) && (tolower(*str
) == 'e'))
106 if (*str
== '+' || *str
== '-')
109 while (isdigit (*str
))
112 exp
= exp
* 10 + (*str
- '0');
121 /* there were no digits after 'e' - rollback pointer */
122 str
--; if (c2
!= 0) str
--;
125 val
*= pow (10, exp
);
131 if ((digits
== 0) && (c
!= 0))
133 /* there were no digits but there was sign - rollback pointer */
139 *endptr
= (char *)str
;
146 void strtod (const char * str
,char ** endptr
)
151 #endif /* AROS_NOFPU */