1 /* Copyright (c) 2002-2005 Michael Stumpf <mistumpf@de.pepperl-fuchs.com>
2 Copyright (c) 2006,2008 Dmitry Xmelkov
3
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8
9 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in
13 the documentation and/or other materials provided with the
14 distribution.
15 * Neither the name of the copyright holders nor the names of
16 contributors may be used to endorse or promote products derived
17 from this software without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 POSSIBILITY OF SUCH DAMAGE. */
30
31 /* $Id: strtod.c 2191 2010-11-05 13:45:57Z arcanum $ */
32
33
34 #include <ctype.h>
35 #include <errno.h>
36 #include <limits.h>
37 #include <math.h> /* INFINITY, NAN */
38 #include <string.h>
39 #include <stdlib.h>
40 #include <inttypes.h>
41
42 /** The strtod() function converts the initial portion of the string pointed
43 to by \a nptr to double representation.
44
45 The expected form of the string is an optional plus ( \c '+' ) or minus
46 sign ( \c '-' ) followed by a sequence of digits optionally containing
47 a decimal-point character, optionally followed by an exponent. An
48 exponent consists of an \c 'E' or \c 'e', followed by an optional plus
49 or minus sign, followed by a sequence of digits.
50
51 Leading white-space characters in the string are skipped.
52
53 The strtod() function returns the converted value, if any.
54
55 If \a endptr is not \c NULL, a pointer to the character after the last
56 character used in the conversion is stored in the location referenced by
57 \a endptr.
58
59 If no conversion is performed, zero is returned and the value of
60 \a nptr is stored in the location referenced by \a endptr.
61
62 If the correct value would cause overflow, plus or minus \c INFINITY is
63 returned (according to the sign of the value), and \c ERANGE is stored
64 in \c errno. If the correct value would cause underflow, zero is
65 returned and \c ERANGE is stored in \c errno.
66 */
67
68 #include "stdio_private.h"
69
70 #define STRTOD
71
72 #include "conv_flt.c"
73
74 double
strtod(const char * nptr,char ** endptr)75 strtod (const char * nptr, char ** endptr)
76 {
77 int len = 0;
78 double flt;
79 unsigned char ret;
80
81 while (ISSPACE(nptr[len]))
82 len++;
83
84 ret = conv_flt(nptr, &len, INT_MAX, &flt, FL_LONG);
85 if (!ret) {
86 flt = 0.0;
87 len = 0;
88 }
89 if (endptr)
90 *endptr = (char *) nptr + len;
91 return flt;
92 }
93
94 #if defined(_HAVE_LONG_DOUBLE) && defined(_LDBL_EQ_DBL)
95 #ifdef _HAVE_ALIAS_ATTRIBUTE
96 #pragma GCC diagnostic ignored "-Wpragmas"
97 #pragma GCC diagnostic ignored "-Wunknown-warning-option"
98 #pragma GCC diagnostic ignored "-Wattribute-alias="
99 extern long double strtold(const char *, char **) __attribute__ ((__alias__ ("strtod")));
100 #else
101 long double
strtold(const char * nptr,char ** endptr)102 strtold (const char * nptr, char ** endptr)
103 {
104 return (long double) strtod(nptr, endptr);
105 }
106 #endif
107 #endif
108