Listtree.mcc: forward List_TestPos and Listtree_TestPos
[AROS.git] / compiler / stdc / strtod.c
blob3e34d29c595224d482236a6466d4e801f60e7c4d
1 /*
2 Copyright © 1995-2012, The AROS Development Team. All rights reserved.
3 $Id$
5 C99 function strtod().
6 */
8 #ifndef AROS_NOFPU
10 #include <ctype.h>
11 #include <limits.h>
13 /*****************************************************************************
15 NAME */
16 #include <stdlib.h>
17 #include <math.h>
19 double strtod (
21 /* SYNOPSIS */
22 const char * str,
23 char ** endptr)
25 /* FUNCTION
26 Convert a string of digits into a double.
28 INPUTS
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
35 here.
37 RESULT
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
41 returned.
43 NOTES
45 EXAMPLE
47 BUGS
48 NAN is not handled at the moment
50 SEE ALSO
51 atof(), atoi(), atol(), strtol(), strtoul()
53 INTERNALS
55 ******************************************************************************/
57 /* Unit tests available in : tests/clib/strtod.c */
58 /* FIXME: implement NAN handling */
59 double val = 0, precision;
60 int exp = 0;
61 char c = 0, c2 = 0;
62 int digits = 0;
64 /* assign initial value in case nothing will be found */
65 if (endptr)
66 *endptr = (char *)str;
68 /* skip all leading spaces */
69 while (isspace (*str))
70 str ++;
72 /* start with scanning the floting point number */
73 if (*str)
75 /* Is there a sign? */
76 if (*str == '+' || *str == '-')
77 c = *str ++;
79 /* scan numbers before the dot */
80 while (isdigit(*str))
82 digits++;
83 val = val * 10 + (*str - '0');
84 str ++;
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)))))
91 str++;
92 /* scan the numbers behind the dot */
93 precision = 0.1;
94 while (isdigit (*str))
96 digits++;
97 val += ((*str - '0') * precision) ;
98 str ++;
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'))
106 int edigits = 0;
107 str++;
109 if (*str == '+' || *str == '-')
110 c2 = *str ++;
112 while (isdigit (*str))
114 edigits++;
115 exp = exp * 10 + (*str - '0');
116 str ++;
119 if (c2 == '-')
120 exp = -exp;
122 if (edigits == 0)
124 /* there were no digits after 'e' - rollback pointer */
125 str--; if (c2 != 0) str--;
128 val *= pow (10, exp);
131 if (c == '-')
132 val = -val;
134 if ((digits == 0) && (c != 0))
136 /* there were no digits but there was sign - rollback pointer */
137 str--;
141 /* something was found, assign the pointer value */
142 if (endptr && digits > 0)
143 *endptr = (char *)str;
145 return val;
146 } /* strtod */
148 #else
150 void strtod (const char * str,char ** endptr)
152 return;
155 #endif /* AROS_NOFPU */