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. */
29 #define DBL_MAX 1.7976931348623159e+308
30 #define DBL_MIN 2.2250738585072010e-308
41 /* Convert NPTR to a double. If ENDPTR is not NULL, a pointer to the
42 character after the last one used in the number is put in *ENDPTR. */
48 register const char *s
;
51 /* The number so far. */
54 int got_dot
; /* Found a decimal point. */
55 int got_digit
; /* Seen any digits. */
57 /* The exponent of the number. */
73 sign
= *s
== '-' ? -1 : 1;
74 if (*s
== '-' || *s
== '+')
87 /* Make sure that multiplication by 10 will not overflow. */
88 if (num
> DBL_MAX
* 0.1)
89 /* The value of the digit doesn't matter, since we have already
90 gotten as many digits as can be represented in a `double'.
91 This doesn't necessarily mean the result will overflow.
92 The exponent may reduce it to within range.
94 We just need to record that there was another
95 digit so that we can multiply by 10 later. */
98 num
= (num
* 10.0) + (*s
- '0');
100 /* Keep track of the number of digits after the decimal point.
101 If we just divided by 10 here, we would lose precision. */
105 else if (!got_dot
&& *s
== '.')
106 /* Record that we have found the decimal point. */
109 /* Any other character terminates the number. */
116 if (tolower (*s
) == 'e')
118 /* Get the exponent specified after the `e' or `E'. */
125 exp
= strtol (s
, &end
, 10);
128 /* The exponent overflowed a `long int'. It is probably a safe
129 assumption that an exponent that cannot be represented by
130 a `long int' exceeds the limits of a `double'. */
139 /* There was no exponent. Reset END to point to
140 the 'e' or 'E', so *ENDPTR will be set there. */
141 end
= (char *) s
- 1;
148 *endptr
= (char *) s
;
153 /* Multiply NUM by 10 to the EXPONENT power,
154 checking for overflow and underflow. */
158 if (num
< DBL_MIN
* pow (10.0, (double) -exponent
))
161 else if (exponent
> 0)
163 if (num
> DBL_MAX
* pow (10.0, (double) -exponent
))
167 num
*= pow (10.0, (double) exponent
);
172 /* Return an overflow error. */
174 return HUGE_VAL
* sign
;
177 /* Return an underflow error. */
179 *endptr
= (char *) nptr
;
184 /* There was no number. */
186 *endptr
= (char *) nptr
;