1 /* Copyright (C) 1996 DJ Delorie, see COPYING.DJ for details */ 2 /* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */ 3 4 #include <precomp.h> 5 6 /* 7 * @implemented 8 */ 9 double 10 strtod(const char *s, char **sret) 11 { 12 long double r; /* result */ 13 int e; /* exponent */ 14 long double d; /* scale */ 15 int sign; /* +- 1.0 */ 16 int esign; 17 int i; 18 int flags=0; 19 20 r = 0.0; 21 sign = 1; 22 e = 0; 23 esign = 1; 24 25 if (s == NULL) 26 return r; 27 28 29 while ((*s == ' ') || (*s == '\t')) 30 s++; 31 32 if (*s == '+') 33 s++; 34 else if (*s == '-') 35 { 36 sign = -1; 37 s++; 38 } 39 40 while ((*s >= '0') && (*s <= '9')) 41 { 42 flags |= 1; 43 r *= 10.0; 44 r += *s - '0'; 45 s++; 46 } 47 48 if (*s == '.') 49 { 50 d = 0.1L; 51 s++; 52 while ((*s >= '0') && (*s <= '9')) 53 { 54 flags |= 2; 55 r += d * (*s - '0'); 56 s++; 57 d *= 0.1L; 58 } 59 } 60 61 if (flags == 0) 62 { 63 if (sret) 64 *sret = (char *)s; 65 return 0; 66 } 67 68 if ((*s == 'e') || (*s == 'E')) 69 { 70 s++; 71 if (*s == '+') 72 s++; 73 else if (*s == '-') 74 { 75 s++; 76 esign = -1; 77 } 78 if ((*s < '0') || (*s > '9')) 79 { 80 if (sret) 81 *sret = (char *)s; 82 return r; 83 } 84 85 while ((*s >= '0') && (*s <= '9')) 86 { 87 e *= 10; 88 e += *s - '0'; 89 s++; 90 } 91 } 92 93 if (esign < 0) 94 for (i = 1; i <= e; i++) 95 r *= 0.1L; 96 else 97 for (i = 1; i <= e; i++) 98 r *= 10.0; 99 100 if (sret) 101 *sret = (char *)s; 102 return r * sign; 103 } 104