Mercurial > hg > octave-nkf > gnulib-hg
annotate lib/xstrtod.c @ 6455:8cf44d5bb070
* xstrtod.c: Don't bother with #pragma STDC FENV_ACCESS ON, as
coreutils no longer futzes with rounding modes.
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Tue, 15 Nov 2005 18:30:06 +0000 |
parents | a48fb0e98c8c |
children | 314715e0260d |
rev | line source |
---|---|
4910 | 1 /* error-checking interface to strtod-like functions |
5159 | 2 |
6455
8cf44d5bb070
* xstrtod.c: Don't bother with #pragma STDC FENV_ACCESS ON, as
Paul Eggert <eggert@cs.ucla.edu>
parents:
5848
diff
changeset
|
3 Copyright (C) 1996, 1999, 2000, 2003, 2004, 2005 Free Software |
8cf44d5bb070
* xstrtod.c: Don't bother with #pragma STDC FENV_ACCESS ON, as
Paul Eggert <eggert@cs.ucla.edu>
parents:
5848
diff
changeset
|
4 Foundation, Inc. |
569 | 5 |
6 This program is free software; you can redistribute it and/or modify | |
7 it under the terms of the GNU General Public License as published by | |
8 the Free Software Foundation; either version 2, or (at your option) | |
9 any later version. | |
10 | |
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. | |
15 | |
16 You should have received a copy of the GNU General Public License | |
570 | 17 along with this program; if not, write to the Free Software Foundation, |
5848
a48fb0e98c8c
*** empty log message ***
Paul Eggert <eggert@cs.ucla.edu>
parents:
5159
diff
changeset
|
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ |
569 | 19 |
20 /* Written by Jim Meyering. */ | |
21 | |
513 | 22 #ifdef HAVE_CONFIG_H |
653 | 23 # include <config.h> |
513 | 24 #endif |
25 | |
4691 | 26 #include "xstrtod.h" |
513 | 27 |
28 #include <errno.h> | |
4691 | 29 #include <limits.h> |
513 | 30 #include <stdio.h> |
31 | |
568 | 32 /* An interface to strtod that encapsulates all the error checking |
569 | 33 one should usually perform. Like strtod, but upon successful |
5159 | 34 conversion put the result in *RESULT and return true. Return |
35 false and don't modify *RESULT upon any failure. CONVERT | |
4910 | 36 specifies the conversion function, e.g., strtod itself. */ |
568 | 37 |
5159 | 38 bool |
4910 | 39 xstrtod (char const *str, char const **ptr, double *result, |
40 double (*convert) (char const *, char **)) | |
513 | 41 { |
42 double val; | |
43 char *terminator; | |
5159 | 44 bool ok = true; |
513 | 45 |
46 errno = 0; | |
4910 | 47 val = convert (str, &terminator); |
513 | 48 |
49 /* Having a non-zero terminator is an error only when PTR is NULL. */ | |
50 if (terminator == str || (ptr == NULL && *terminator != '\0')) | |
5159 | 51 ok = false; |
513 | 52 else |
53 { | |
54 /* Allow underflow (in which case strtod returns zero), | |
55 but flag overflow as an error. */ | |
56 if (val != 0.0 && errno == ERANGE) | |
5159 | 57 ok = false; |
513 | 58 } |
59 | |
60 if (ptr != NULL) | |
61 *ptr = terminator; | |
62 | |
63 *result = val; | |
5159 | 64 return ok; |
513 | 65 } |