strtod.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /* Copyright (C) 1991, 1992, 1997, 1999 Free Software Foundation, Inc.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 2, or (at your option)
  5. any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software Foundation,
  12. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  13. #if HAVE_CONFIG_H
  14. # include <config.h>
  15. #endif
  16. #include <errno.h>
  17. #ifndef errno
  18. extern int errno;
  19. #endif
  20. #include <ctype.h>
  21. #if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
  22. # define IN_CTYPE_DOMAIN(c) 1
  23. #else
  24. # define IN_CTYPE_DOMAIN(c) isascii(c)
  25. #endif
  26. #define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))
  27. #define ISDIGIT(c) (IN_CTYPE_DOMAIN (c) && isdigit (c))
  28. #define TOLOWER(c) (IN_CTYPE_DOMAIN (c) ? tolower(c) : (c))
  29. #include <math.h>
  30. #if HAVE_FLOAT_H
  31. # include <float.h>
  32. #else
  33. # define DBL_MAX 1.7976931348623159e+308
  34. # define DBL_MIN 2.2250738585072010e-308
  35. #endif
  36. #if STDC_HEADERS
  37. # include <stdlib.h>
  38. # include <string.h>
  39. #else
  40. # define NULL 0
  41. # ifndef HUGE_VAL
  42. # define HUGE_VAL HUGE
  43. # endif
  44. #endif
  45. /* Convert NPTR to a double. If ENDPTR is not NULL, a pointer to the
  46. character after the last one used in the number is put in *ENDPTR. */
  47. double
  48. strtod (const char *nptr, char **endptr)
  49. {
  50. register const char *s;
  51. short int sign;
  52. /* The number so far. */
  53. double num;
  54. int got_dot; /* Found a decimal point. */
  55. int got_digit; /* Seen any digits. */
  56. /* The exponent of the number. */
  57. long int exponent;
  58. if (nptr == NULL)
  59. {
  60. errno = EINVAL;
  61. goto noconv;
  62. }
  63. s = nptr;
  64. /* Eat whitespace. */
  65. while (ISSPACE (*s))
  66. ++s;
  67. /* Get the sign. */
  68. sign = *s == '-' ? -1 : 1;
  69. if (*s == '-' || *s == '+')
  70. ++s;
  71. num = 0.0;
  72. got_dot = 0;
  73. got_digit = 0;
  74. exponent = 0;
  75. for (;; ++s)
  76. {
  77. if (ISDIGIT (*s))
  78. {
  79. got_digit = 1;
  80. /* Make sure that multiplication by 10 will not overflow. */
  81. if (num > DBL_MAX * 0.1)
  82. /* The value of the digit doesn't matter, since we have already
  83. gotten as many digits as can be represented in a `double'.
  84. This doesn't necessarily mean the result will overflow.
  85. The exponent may reduce it to within range.
  86. We just need to record that there was another
  87. digit so that we can multiply by 10 later. */
  88. ++exponent;
  89. else
  90. num = (num * 10.0) + (*s - '0');
  91. /* Keep track of the number of digits after the decimal point.
  92. If we just divided by 10 here, we would lose precision. */
  93. if (got_dot)
  94. --exponent;
  95. }
  96. else if (!got_dot && *s == '.')
  97. /* Record that we have found the decimal point. */
  98. got_dot = 1;
  99. else
  100. /* Any other character terminates the number. */
  101. break;
  102. }
  103. if (!got_digit)
  104. goto noconv;
  105. if (TOLOWER (*s) == 'e')
  106. {
  107. /* Get the exponent specified after the `e' or `E'. */
  108. int save = errno;
  109. char *end;
  110. long int exp;
  111. errno = 0;
  112. ++s;
  113. exp = strtol (s, &end, 10);
  114. if (errno == ERANGE)
  115. {
  116. /* The exponent overflowed a `long int'. It is probably a safe
  117. assumption that an exponent that cannot be represented by
  118. a `long int' exceeds the limits of a `double'. */
  119. if (endptr != NULL)
  120. *endptr = end;
  121. if (exp < 0)
  122. goto underflow;
  123. else
  124. goto overflow;
  125. }
  126. else if (end == s)
  127. /* There was no exponent. Reset END to point to
  128. the 'e' or 'E', so *ENDPTR will be set there. */
  129. end = (char *) s - 1;
  130. errno = save;
  131. s = end;
  132. exponent += exp;
  133. }
  134. if (endptr != NULL)
  135. *endptr = (char *) s;
  136. if (num == 0.0)
  137. return 0.0;
  138. /* Multiply NUM by 10 to the EXPONENT power,
  139. checking for overflow and underflow. */
  140. if (exponent < 0)
  141. {
  142. if (num < DBL_MIN * pow (10.0, (double) -exponent))
  143. goto underflow;
  144. }
  145. else if (exponent > 0)
  146. {
  147. if (num > DBL_MAX * pow (10.0, (double) -exponent))
  148. goto overflow;
  149. }
  150. num *= pow (10.0, (double) exponent);
  151. return num * sign;
  152. overflow:
  153. /* Return an overflow error. */
  154. errno = ERANGE;
  155. return HUGE_VAL * sign;
  156. underflow:
  157. /* Return an underflow error. */
  158. if (endptr != NULL)
  159. *endptr = (char *) nptr;
  160. errno = ERANGE;
  161. return 0.0;
  162. noconv:
  163. /* There was no number. */
  164. if (endptr != NULL)
  165. *endptr = (char *) nptr;
  166. return 0.0;
  167. }