Autodoc corrections
[cake.git] / compiler / clib / strtod.c
blobdfe021c560d3730354f097ec08114ba431ef5d29
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
50 SEE ALSO
51 atof(), atoi(), atol(), strtol(), strtoul()
53 INTERNALS
55 ******************************************************************************/
57 double val = 0, precision;
58 int exp = 0;
59 char c = 0, c2 = 0;
61 /* skip all leading spaces */
62 while (isspace (*str))
63 str ++;
65 /* start with scanning the floting point number */
66 if (*str)
68 /* Is there a sign? */
69 if (*str == '+' || *str == '-')
70 c = *str ++;
72 /* scan numbers before the dot */
73 while (isdigit(*str))
75 val = val * 10 + (*str - '0');
76 str ++;
79 /* see if there is the dot */
80 if(*str == '.')
82 str++;
83 /* scan the numbers behind the dot */
84 precision = 0.1;
85 while (isdigit (*str))
87 val += ((*str - '0') * precision) ;
88 str ++;
89 precision = precision * 0.1;
93 /* look for a sequence like "E+10" or "e-22" */
94 if(tolower(*str) == 'e')
96 str++;
98 if (*str == '+' || *str == '-')
99 c2 = *str ++;
101 while (isdigit (*str))
103 exp = exp * 10 + (*str - '0');
104 str ++;
106 if (c2 == '-')
107 exp = -exp;
108 val *= pow (10, exp);
111 if (c == '-')
112 val = -val;
115 if (endptr)
116 *endptr = (char *)str;
118 return val;
119 } /* strtod */
121 #else
123 void strtod (const char * str,char ** endptr)
125 return;
128 #endif /* AROS_NOFPU */