5 static int valid_exp(const unsigned char *s)
7 return isdigit(*s) || ((s[0]=='+'||s[0]=='-') && isdigit(s[1]));
10 long double strtold(const char *s1, char **p)
12 const unsigned char *s = (void *)s1;
19 int saved_errno = errno;
21 if (!p) p = (char **)&s1;
23 /* Initial whitespace */
24 for (; isspace(*s); s++);
27 if (*s == '-') sign = *s++;
28 else if (*s == '+') s++;
30 /* Handle infinities and NaNs. */
31 if ((s[0]|32)=='i' && (s[1]|32)=='n' && (s[2]|32)=='f') {
33 return sign ? -1.0/0.0 : 1.0/0.0;
34 } else if ((s[0]|32)=='n' && (s[1]|32)=='a' && (s[2]|32)=='n') {
39 /* Possible hex float */
40 if (s[0]=='0' && (s[1]|32)=='x') {
41 /* Mantissa must be non-degenerate */
42 if (!isxdigit(s[2]) && (s[2]!=radix || !isxdigit(s[3]))) {
43 /* Decimal float 0, 'x' extraneous */
47 /* We have a real hex float */
49 for (; isxdigit(*s); s++) {
50 x = 16*x + (isdigit(*s)?*s-'0':(*s|32)-'a'+10);
51 if (*s!='0') nonzero=1;
55 for (s++; isxdigit(*s); s++) {
56 x += frac * (isdigit(*s)?*s-'0':(*s|32)-'a'+10);
58 if (*s!='0') nonzero=1;
61 if ((*s|32) == 'p' && valid_exp(s+1)) {
62 e = strtol((void *)(s+1), (void *)&s, 10);
63 for (; e>0; e--) x *= 2.0;
64 for (; e<0; e++) x *= 0.5;
69 /* Mantissa must be non-degenerate */
70 if (!isdigit(s[0]) && (s[0]!=radix || !isdigit(s[1]))) {
75 for (; isdigit(*s); s++) {
77 if (*s!='0') nonzero=1;
81 for (s++; isdigit(*s); s++) {
84 if (*s!='0') nonzero=1;
87 if ((*s|32)=='e' && valid_exp(s+1)) {
88 e = strtol((void *)++s, (void *)&s, 10);
89 for (; e>0; e--) x *= 10.0;
90 for (; e<0; e++) x /= 10.0;
93 errno = ((nonzero && !x) || !(1.0/x)) ? ERANGE : saved_errno;