1 /* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
20 #include <localeinfo.h>
29 /* Convert NPTR to a double. If ENDPTR is not NULL, a pointer to the
30 character after the last one used in the number is put in *ENDPTR. */
32 DEFUN(strtod, (nptr, endptr), CONST char *nptr AND char **endptr)
34 register CONST char *s;
36 wchar_t decimal; /* Decimal point character. */
38 /* The number so far. */
41 int got_dot; /* Found a decimal point. */
42 int got_digit; /* Seen any digits. */
44 /* The exponent of the number. */
53 /* Figure out the decimal point character. */
54 if (mbtowc(&decimal, _numeric_info->decimal_point, 1) <= 0)
55 decimal = (wchar_t) *_numeric_info->decimal_point;
64 sign = *s == '-' ? -1 : 1;
65 if (*s == '-' || *s == '+')
78 /* Make sure that multiplication by 10 will not overflow. */
79 if (num > DBL_MAX * 0.1)
80 /* The value of the digit doesn't matter, since we have already
81 gotten as many digits as can be represented in a `double'.
82 This doesn't necessarily mean the result will overflow.
83 The exponent may reduce it to within range.
85 We just need to record that there was another
86 digit so that we can multiply by 10 later. */
89 num = (num * 10.0) + (*s - '0');
91 /* Keep track of the number of digits after the decimal point.
92 If we just divided by 10 here, we would lose precision. */
96 else if (!got_dot && (wchar_t) *s == decimal)
97 /* Record that we have found the decimal point. */
100 /* Any other character terminates the number. */
107 if (tolower(*s) == 'e')
109 /* Get the exponent specified after the `e' or `E'. */
116 exp = strtol(s, &end, 10);
119 /* The exponent overflowed a `long int'. It is probably a safe
120 assumption that an exponent that cannot be represented by
121 a `long int' exceeds the limits of a `double'. */
130 /* There was no exponent. Reset END to point to
131 the 'e' or 'E', so *ENDPTR will be set there. */
132 end = (char *) s - 1;
139 *endptr = (char *) s;
144 /* Multiply NUM by 10 to the EXPONENT power,
145 checking for overflow and underflow. */
149 if (num < DBL_MIN * pow(10.0, (double) -exponent))
152 else if (exponent > 0)
154 if (num > DBL_MAX * pow(10.0, (double) -exponent))
158 num *= pow(10.0, (double) exponent);
163 /* Return an overflow error. */
165 return HUGE_VAL * sign;
168 /* Return an underflow error. */
170 *endptr = (char *) nptr;
175 /* There was no number. */
177 *endptr = (char *) nptr;