1 /* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
3 NOTE: The canonical source of this file is maintained with the GNU C Library.
4 Bugs can be reported to bug-glibc@prep.ai.mit.edu.
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 #include <localeinfo.h>
30 /* Convert NPTR to a double. If ENDPTR is not NULL, a pointer to the
31 character after the last one used in the number is put in *ENDPTR. */
33 DEFUN(strtod, (nptr, endptr), CONST char *nptr AND char **endptr)
35 register CONST char *s;
37 wchar_t decimal; /* Decimal point character. */
39 /* The number so far. */
42 int got_dot; /* Found a decimal point. */
43 int got_digit; /* Seen any digits. */
45 /* The exponent of the number. */
54 /* Figure out the decimal point character. */
55 if (mbtowc(&decimal, _numeric_info->decimal_point, 1) <= 0)
56 decimal = (wchar_t) *_numeric_info->decimal_point;
65 sign = *s == '-' ? -1 : 1;
66 if (*s == '-' || *s == '+')
79 /* Make sure that multiplication by 10 will not overflow. */
80 if (num > DBL_MAX * 0.1)
81 /* The value of the digit doesn't matter, since we have already
82 gotten as many digits as can be represented in a `double'.
83 This doesn't necessarily mean the result will overflow.
84 The exponent may reduce it to within range.
86 We just need to record that there was another
87 digit so that we can multiply by 10 later. */
90 num = (num * 10.0) + (*s - '0');
92 /* Keep track of the number of digits after the decimal point.
93 If we just divided by 10 here, we would lose precision. */
97 else if (!got_dot && (wchar_t) *s == decimal)
98 /* Record that we have found the decimal point. */
101 /* Any other character terminates the number. */
108 if (tolower(*s) == 'e')
110 /* Get the exponent specified after the `e' or `E'. */
117 exp = strtol(s, &end, 10);
120 /* The exponent overflowed a `long int'. It is probably a safe
121 assumption that an exponent that cannot be represented by
122 a `long int' exceeds the limits of a `double'. */
131 /* There was no exponent. Reset END to point to
132 the 'e' or 'E', so *ENDPTR will be set there. */
133 end = (char *) s - 1;
140 *endptr = (char *) s;
145 /* Multiply NUM by 10 to the EXPONENT power,
146 checking for overflow and underflow. */
150 if (num < DBL_MIN * pow(10.0, (double) -exponent))
153 else if (exponent > 0)
155 if (num > DBL_MAX * pow(10.0, (double) -exponent))
159 num *= pow(10.0, (double) exponent);
164 /* Return an overflow error. */
166 return HUGE_VAL * sign;
169 /* Return an underflow error. */
171 *endptr = (char *) nptr;
176 /* There was no number. */
178 *endptr = (char *) nptr;