Mercurial > hg > octave-nkf > gnulib-hg
annotate lib/strtod.c @ 7302:8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
longer worry about uses that don't define HAVE_CONFIG_H.
* acl.c, alloca.c, argmatch.c, atexit.c, backupfile.c:
* basename.c, c-stack.c, c-strtod.c, calloc.c, canon-host.c:
* canonicalize.c, chdir-long.c, chdir-safer.c, chown.c:
* cloexec.c, close-stream.c, closeout.c, creat-safer.c:
* cycle-check.c, diacrit.c, dirchownmod.c, dirfd.c, dirname.c:
* dup-safer.c, dup2.c, error.c, euidaccess.c, exclude.c:
* exitfail.c, fchmodat.c, fchown-stub.c, fd-safer.c:
* file-type.c, fileblocks.c, filemode.c, filenamecat.c:
* fnmatch.c, fopen-safer.c, fprintftime.c, free.c, fsusage.c:
* ftruncate.c, fts-cycle.c, fts.c, full-write.c, gai_strerror.c:
* getcwd.c, getdate.y, getdomainname.c, getgroups.c:
* gethostname.c, gethrxtime.c, getloadavg.c, getlogin_r.c:
* getndelim2.c, getnline.c, getopt.c, getopt1.c, getpass.c:
* gettime.c, gettimeofday.c, getugroups.c, getusershell.c:
* glob.c, group-member.c, hard-locale.c, hash-pjw.c, hash.c:
* human.c, idcache.c, inet_ntop.c, inet_pton.c, inttostr.c:
* isdir.c, lchown.c, linebuffer.c, long-options.c, lstat.c:
* malloc.c, md5.c, memcasecmp.c, memchr.c, memcmp.c, memcoll.c:
* memcpy.c, memmove.c, memrchr.c, mkancesdirs.c, mkdir-p.c:
* mkdir.c, mkdirat.c, mkstemp-safer.c, mkstemp.c, modechange.c:
* mountlist.c, nanosleep.c, obstack.c, open-safer.c:
* openat-die.c, openat.c, pagealign_alloc.c, physmem.c:
* pipe-safer.c, posixtm.c, posixver.c, putenv.c, quote.c:
* quotearg.c, raise.c, readtokens.c, readtokens0.c, readutmp.c:
* realloc.c, regex.c, rename.c, rmdir.c, rpmatch.c, safe-read.c:
* same.c, save-cwd.c, savedir.c, setenv.c, settime.c, sha1.c:
* sig2str.c, snprintf.c, strdup.c, strerror.c, strftime.c:
* stripslash.c, strndup.c, strnlen.c, strpbrk.c, strtod.c:
* strtoimax.c, strtol.c, strverscmp.c, tempname.c, time_r.c:
* timegm.c, tmpfile-safer.c, unlinkdir.c, userspec.c, utime.c:
* utimecmp.c, utimens.c, version-etc-fsf.c, version-etc.c:
* xalloc-die.c, xgetcwd.c, xgethostname.c, xmalloc.c:
* xmemcoll.c, xnanosleep.c, xreadlink.c, xstrtod.c:
* xstrtoimax.c, xstrtol.c, xstrtoumax.c, yesno.c:
Likewise.
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Wed, 13 Sep 2006 22:38:14 +0000 |
parents | 188d89808804 |
children | bbbbbf4cd1c5 |
rev | line source |
---|---|
6834
8094e7d61c3b
* strtod.c [!defined errno]: Assume errno.h declares errno.
Eric Blake <ebb9@byu.net>
parents:
6259
diff
changeset
|
1 /* Copyright (C) 1991, 1992, 1997, 1999, 2003, 2006 Free Software Foundation, Inc. |
9 | 2 |
311 | 3 This program is free software; you can redistribute it and/or modify |
4 it under the terms of the GNU General Public License as published by | |
5 the Free Software Foundation; either version 2, or (at your option) | |
6 any later version. | |
9 | 7 |
311 | 8 This program is distributed in the hope that it will be useful, |
9 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 GNU General Public License for more details. | |
12 | |
13 You should have received a copy of the GNU General Public License | |
649
89f4c1937ac7
update FSF address in copyright and remove any trailing blanks
Jim Meyering <jim@meyering.net>
parents:
373
diff
changeset
|
14 along with this program; if not, write to the Free Software Foundation, |
5848
a48fb0e98c8c
*** empty log message ***
Paul Eggert <eggert@cs.ucla.edu>
parents:
4691
diff
changeset
|
15 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ |
309 | 16 |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
6930
diff
changeset
|
17 #include <config.h> |
311 | 18 |
19 #include <errno.h> | |
373 | 20 |
311 | 21 #include <ctype.h> |
1042 | 22 |
311 | 23 #include <math.h> |
9 | 24 |
4691 | 25 #include <float.h> |
26 #include <stdlib.h> | |
27 #include <string.h> | |
9 | 28 |
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. */ | |
31 double | |
1691 | 32 strtod (const char *nptr, char **endptr) |
9 | 33 { |
311 | 34 register const char *s; |
9 | 35 short int sign; |
36 | |
37 /* The number so far. */ | |
38 double num; | |
39 | |
311 | 40 int got_dot; /* Found a decimal point. */ |
41 int got_digit; /* Seen any digits. */ | |
9 | 42 |
43 /* The exponent of the number. */ | |
44 long int exponent; | |
45 | |
46 if (nptr == NULL) | |
47 { | |
48 errno = EINVAL; | |
49 goto noconv; | |
50 } | |
51 | |
52 s = nptr; | |
53 | |
54 /* Eat whitespace. */ | |
6927
fa896bb33133
* lib/memcasecmp.c: Include <limits.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
6834
diff
changeset
|
55 while (isspace ((unsigned char) *s)) |
9 | 56 ++s; |
57 | |
58 /* Get the sign. */ | |
59 sign = *s == '-' ? -1 : 1; | |
60 if (*s == '-' || *s == '+') | |
61 ++s; | |
62 | |
63 num = 0.0; | |
64 got_dot = 0; | |
65 got_digit = 0; | |
66 exponent = 0; | |
67 for (;; ++s) | |
68 { | |
6927
fa896bb33133
* lib/memcasecmp.c: Include <limits.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
6834
diff
changeset
|
69 if ('0' <= *s && *s <= '9') |
9 | 70 { |
71 got_digit = 1; | |
72 | |
73 /* Make sure that multiplication by 10 will not overflow. */ | |
74 if (num > DBL_MAX * 0.1) | |
75 /* The value of the digit doesn't matter, since we have already | |
76 gotten as many digits as can be represented in a `double'. | |
77 This doesn't necessarily mean the result will overflow. | |
78 The exponent may reduce it to within range. | |
79 | |
80 We just need to record that there was another | |
81 digit so that we can multiply by 10 later. */ | |
82 ++exponent; | |
83 else | |
84 num = (num * 10.0) + (*s - '0'); | |
85 | |
86 /* Keep track of the number of digits after the decimal point. | |
87 If we just divided by 10 here, we would lose precision. */ | |
88 if (got_dot) | |
89 --exponent; | |
90 } | |
311 | 91 else if (!got_dot && *s == '.') |
9 | 92 /* Record that we have found the decimal point. */ |
93 got_dot = 1; | |
94 else | |
95 /* Any other character terminates the number. */ | |
96 break; | |
97 } | |
98 | |
99 if (!got_digit) | |
100 goto noconv; | |
101 | |
6930
188d89808804
* strtod.c (strtod): cast the argument of tolower to unsigned char.
Ralf Wildenhues <Ralf.Wildenhues@gmx.de>
parents:
6927
diff
changeset
|
102 if (tolower ((unsigned char) *s) == 'e') |
9 | 103 { |
104 /* Get the exponent specified after the `e' or `E'. */ | |
105 int save = errno; | |
106 char *end; | |
107 long int exp; | |
108 | |
109 errno = 0; | |
110 ++s; | |
311 | 111 exp = strtol (s, &end, 10); |
9 | 112 if (errno == ERANGE) |
113 { | |
114 /* The exponent overflowed a `long int'. It is probably a safe | |
115 assumption that an exponent that cannot be represented by | |
116 a `long int' exceeds the limits of a `double'. */ | |
117 if (endptr != NULL) | |
118 *endptr = end; | |
119 if (exp < 0) | |
120 goto underflow; | |
121 else | |
122 goto overflow; | |
123 } | |
124 else if (end == s) | |
125 /* There was no exponent. Reset END to point to | |
126 the 'e' or 'E', so *ENDPTR will be set there. */ | |
127 end = (char *) s - 1; | |
128 errno = save; | |
129 s = end; | |
130 exponent += exp; | |
131 } | |
132 | |
133 if (endptr != NULL) | |
134 *endptr = (char *) s; | |
135 | |
136 if (num == 0.0) | |
137 return 0.0; | |
138 | |
139 /* Multiply NUM by 10 to the EXPONENT power, | |
140 checking for overflow and underflow. */ | |
141 | |
142 if (exponent < 0) | |
143 { | |
311 | 144 if (num < DBL_MIN * pow (10.0, (double) -exponent)) |
9 | 145 goto underflow; |
146 } | |
147 else if (exponent > 0) | |
148 { | |
311 | 149 if (num > DBL_MAX * pow (10.0, (double) -exponent)) |
9 | 150 goto overflow; |
151 } | |
152 | |
311 | 153 num *= pow (10.0, (double) exponent); |
9 | 154 |
155 return num * sign; | |
156 | |
311 | 157 overflow: |
9 | 158 /* Return an overflow error. */ |
159 errno = ERANGE; | |
160 return HUGE_VAL * sign; | |
161 | |
311 | 162 underflow: |
9 | 163 /* Return an underflow error. */ |
164 if (endptr != NULL) | |
165 *endptr = (char *) nptr; | |
166 errno = ERANGE; | |
167 return 0.0; | |
168 | |
311 | 169 noconv: |
9 | 170 /* There was no number. */ |
171 if (endptr != NULL) | |
172 *endptr = (char *) nptr; | |
173 return 0.0; | |
174 } |