kernel/src/klibc/klibc.c
double katof(char * string) {
    if (!string) return 0.0;

    double integer = 0.0;        // before decimal
    double fraction = 0.0;       // after decimal
    int sign = 1;                // positive or negative?
    int divisor = 1;             // used to push fraction past decimal point
    bool after_decimal = false;  // decimal point reached?

    // Check if string includes sign (including a "+")
    if (*string == '-') {
        sign = -1;
        string++;  // progress to next char
    } else if (*string == '+') {
        string++;
    }

    while (*string != '\0') {
        if (*string >= '0' && *string <= '9') {
            if (after_decimal) {
                fraction *= 10;               // progress to next position in integer
                fraction += (*string - '0');  // add integer form of current number in string
                divisor *= 10;
            } else {
                integer *= 10;               // progress to next position in integer
                integer += (*string - '0');  // add integer form of current number in string
            }
        } else if (*string == '.') {
            if (after_decimal) return 0.0;  // more than one '.'
            after_decimal = true;
        } else {
            return 0.0;  // current char in string is not a number or '.'
        }
        string++;
    }
    return sign * (integer + (fraction / divisor));
}
// Return string converted to double form, or 0 if not applicable
Return string converted to double form, or 0 if not applicable
Return string converted to double form or 0 if not applicable
double katof(char * string) {
    if (!string) return 0.0;
    double integer = 0.0;        
    double fraction = 0.0;       
    int sign = 1;                
    int divisor = 1;             
    bool after_decimal = false;  
    if (*string == '-') {
        sign = -1;
        string++;  
    } else if (*string == '+') {
        string++;
    }
    while (*string != '\0') {
        if (*string >= '0' && *string <= '9') {
            if (after_decimal) {
                fraction *= 10;               
                fraction += (*string - '0');  
                divisor *= 10;
            } else {
                integer *= 10;               
                integer += (*string - '0');  
            }
        } else if (*string == '.') {
            if (after_decimal) return 0.0;  
            after_decimal = true;
        } else {
            return 0.0;  
        }
        string++;
    }
    return sign * (integer + (fraction / divisor));
}
double katof ( char * string ) { if ( ! string ) return 0.0 ; double integer = 0.0 ; double fraction = 0.0 ; int sign = 1 ; int divisor = 1 ; bool after_decimal = false ; if ( * string == ' ' ) { sign = -1 ; string ++ ; } else if ( * string == ' ' ) { string ++ ; } while ( * string != ' \0 ' ) { if ( * string >= ' ' && * string <= ' ' ) { if ( after_decimal ) { fraction *= 10 ; fraction += ( * string - ' ' ) ; divisor *= 10 ; } else { integer *= 10 ; integer += ( * string - ' ' ) ; } } else if ( * string == ' ' ) { if ( after_decimal ) return 0.0 ; after_decimal = true ; } else { return 0.0 ; } string ++ ; } return sign * ( integer + ( fraction / divisor ) ) ; }
Return string converted to double form, or 0 if not applicable
Return string converted to double form or 0 if not applicable
// before decimal // after decimal // positive or negative? // used to push fraction past decimal point // decimal point reached? // Check if string includes sign (including a "+") // progress to next char // progress to next position in integer // add integer form of current number in string // progress to next position in integer // add integer form of current number in string // more than one '.' // current char in string is not a number or '.'