use same location as .configured, etc, to store .files-touched
[AROS.git] / compiler / clib / strtod.c
blob658f559df7599e74f09f6a88b7b9601a1fb824c5
1 /*
2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
3 $Id$
5 ANSI C function strtod().
6 */
8 #ifndef AROS_NOFPU
10 #include <ctype.h>
11 #include <errno.h>
12 #include <limits.h>
14 /*****************************************************************************
16 NAME */
17 #include <stdlib.h>
18 #include <math.h>
20 double strtod (
22 /* SYNOPSIS */
23 const char * str,
24 char ** endptr)
26 /* FUNCTION
27 Convert a string of digits into a double.
29 INPUTS
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
36 here.
38 RESULT
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
42 returned.
44 NOTES
46 EXAMPLE
48 BUGS
49 NAN is not handled at the moment
51 SEE ALSO
52 atof(), atoi(), atol(), strtol(), strtoul()
54 INTERNALS
56 ******************************************************************************/
58 /* Unit tests available in : tests/clib/strtod.c */
59 /* FIXME: implement NAN handling */
60 double val = 0, precision;
61 int exp = 0;
62 char c = 0, c2 = 0;
63 int digits = 0;
65 /* skip all leading spaces */
66 while (isspace (*str))
67 str ++;
69 /* start with scanning the floting point number */
70 if (*str)
72 /* Is there a sign? */
73 if (*str == '+' || *str == '-')
74 c = *str ++;
76 /* scan numbers before the dot */
77 while (isdigit(*str))
79 digits++;
80 val = val * 10 + (*str - '0');
81 str ++;
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)))))
88 str++;
89 /* scan the numbers behind the dot */
90 precision = 0.1;
91 while (isdigit (*str))
93 digits++;
94 val += ((*str - '0') * precision) ;
95 str ++;
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'))
103 int edigits = 0;
104 str++;
106 if (*str == '+' || *str == '-')
107 c2 = *str ++;
109 while (isdigit (*str))
111 edigits++;
112 exp = exp * 10 + (*str - '0');
113 str ++;
116 if (c2 == '-')
117 exp = -exp;
119 if (edigits == 0)
121 /* there were no digits after 'e' - rollback pointer */
122 str--; if (c2 != 0) str--;
125 val *= pow (10, exp);
128 if (c == '-')
129 val = -val;
131 if ((digits == 0) && (c != 0))
133 /* there were no digits but there was sign - rollback pointer */
134 str--;
138 if (endptr)
139 *endptr = (char *)str;
141 return val;
142 } /* strtod */
144 #else
146 void strtod (const char * str,char ** endptr)
148 return;
151 #endif /* AROS_NOFPU */