strtod.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * strtod.c - This file is part of the libc-8086 package for ELKS,
  3. * Copyright (C) 1995, 1996 Nat Friedman <ndf@linux.mit.edu>.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Library General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Library General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Library General Public
  16. * License along with this library; if not, write to the Free
  17. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. *
  19. */
  20. #include <stdlib.h>
  21. #include <ctype.h>
  22. float
  23. strtod(const char *nptr, char ** endptr)
  24. {
  25. unsigned short negative;
  26. float number;
  27. float fp_part;
  28. int exponent;
  29. unsigned short exp_negative;
  30. /* advance beyond any leading whitespace */
  31. while (isspace(*nptr))
  32. nptr++;
  33. /* check for optional '+' or '-' */
  34. negative=0;
  35. if (*nptr=='-')
  36. {
  37. negative=1;
  38. nptr++;
  39. }
  40. else
  41. if (*nptr=='+')
  42. nptr++;
  43. number=0;
  44. while (isdigit(*nptr))
  45. {
  46. number=number*10+(*nptr-'0');
  47. nptr++;
  48. }
  49. if (*nptr=='.')
  50. {
  51. nptr++;
  52. fp_part=0;
  53. while (isdigit(*nptr))
  54. {
  55. fp_part=fp_part/10.0 + (*nptr-'0')/10.0;
  56. nptr++;
  57. }
  58. number+=fp_part;
  59. }
  60. if (*nptr=='e' || *nptr=='E')
  61. {
  62. nptr++;
  63. exp_negative=0;
  64. if (*nptr=='-')
  65. {
  66. exp_negative=1;
  67. nptr++;
  68. }
  69. else
  70. if (*nptr=='+')
  71. nptr++;
  72. exponent=0;
  73. while (isdigit(*nptr))
  74. {
  75. exponent=exponent*10+(*nptr-'0');
  76. exponent++;
  77. }
  78. }
  79. while (exponent)
  80. {
  81. if (exp_negative)
  82. number/=10;
  83. else
  84. number*=10;
  85. exponent--;
  86. }
  87. return (negative ? -number:number);
  88. }