#include #include #include #include #ifdef isdigit #undef isdigit #endif #define isdigit(c) ((c) >= '0' && (c) <= '9') double strtod__(const char *start, char **end); int main() { // Test case char *end, *start = " -12.1 -0.11 124 24.7 212.193"; end = start; while (*start) { printf("Strtod__ is : %f\n", strtod__(start, &end)); // printf("Strtod is : %f\n", strtod(start, &end)); printf("end is : %s\n", end); // prevent infinity loop if (start == end) break; start = end; } return 0; } // Should avoid floating-point operations because // some processors doesn't has fpu unit // This causes too big performance degradation on arm double strtod__(const char *start, char **end){ printf("Shit start: %s\n", start); // skip leading space characters while (*start && *start == ' ') { start++; } // is bitwise optation more quick? int negate = 1; if(*start == '-'){ negate = -1; start++; } printf("Shit negate: %d\n", negate); printf("Shit start: %s\n\n\n", start); if(*start == '+') start++; // sucking integer part int retvalue = 0; while(isdigit(*start)){ // printf("sucking start: %s\n", start); retvalue *= 10; retvalue = retvalue + (*start - '0'); start++; } // printf("Parsed integer part value: %d and start: %s\n", retvalue, start); if(*start != '.'){ *end = (char *)start; return negate * (double)retvalue; } else start++; // sucking decimal fraction part int fpsize = 1, fpvalue = 0; while(isdigit(*start)){ fpsize *= 10; fpvalue *= 10; fpvalue = fpvalue + (*start - '0'); start++; } *end = (char *)start; // printf("Before return start : %s\n", start); // printf("and fpvalue: %f\n", retvalue + fpvalue * 1.0/fpsize); return negate * (retvalue + fpvalue * (1.0/fpsize)); }