Rev | Line | |
---|
[1] | 1 | #include <string.h> |
---|
| 2 | #include <stdlib.h> |
---|
| 3 | #include <ctype.h> |
---|
| 4 | |
---|
| 5 | double strtod(const char* s, char** endptr) { |
---|
| 6 | register const char* p = s; |
---|
| 7 | register long double value = 0.L; |
---|
| 8 | double sign = +1; |
---|
| 9 | long double factor; |
---|
| 10 | unsigned int expo; |
---|
| 11 | |
---|
| 12 | while ( isspace(*p) ) |
---|
| 13 | p++; |
---|
| 14 | |
---|
| 15 | switch (*p) { |
---|
| 16 | case '-': sign = -1; |
---|
| 17 | case '+': p++; |
---|
| 18 | default : break; |
---|
| 19 | } |
---|
| 20 | |
---|
| 21 | while ( (unsigned int)(*p - '0') < 10u ) |
---|
| 22 | value = value*10 + (*p++ - '0'); |
---|
| 23 | |
---|
| 24 | if ( *p == '.' ) { |
---|
| 25 | factor = 1.; |
---|
| 26 | |
---|
| 27 | p++; |
---|
| 28 | while ( (unsigned int)(*p - '0') < 10u ) { |
---|
| 29 | factor *= 0.1; |
---|
| 30 | value += (*p++ - '0') * factor; |
---|
| 31 | } |
---|
| 32 | } |
---|
| 33 | |
---|
| 34 | if ( (*p | 32) == 'e' ) { |
---|
| 35 | expo = 0; |
---|
| 36 | factor = 10.L; |
---|
| 37 | |
---|
| 38 | switch (*++p) { // ja hier weiß ich nicht, was mindestens nach einem 'E' folgenden MUSS. |
---|
| 39 | case '-': factor = 0.1; |
---|
| 40 | case '+': p++; |
---|
| 41 | break; |
---|
| 42 | case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': |
---|
| 43 | break; |
---|
| 44 | default : value = 0.L; |
---|
| 45 | p = s; |
---|
| 46 | goto done; |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | while ( (unsigned int)(*p - '0') < 10u ) |
---|
| 50 | expo = 10 * expo + (*p++ - '0'); |
---|
| 51 | |
---|
| 52 | while ( 1 ) { |
---|
| 53 | if ( expo & 1 ) |
---|
| 54 | value *= factor; |
---|
| 55 | if ( (expo >>= 1) == 0 ) |
---|
| 56 | break; |
---|
| 57 | factor *= factor; |
---|
| 58 | } |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | done: |
---|
| 62 | if ( endptr != NULL ) |
---|
| 63 | *endptr = (char*)p; |
---|
| 64 | |
---|
| 65 | return value * sign; |
---|
| 66 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.