William Lane wrote:
Well being a newbie to the microcontroller world I am learning that you have to find more efficient ways of figuring things out. So to answer my own question: strtok the decimal, count the decimal places, combine the parsed sections, atoi/atol and then use fixed point math.

Here is what I did for stof, basically modified atoi, does not work with 1.0e4 but could be made to.

unsigned char IsFDigit(char c)
{
    return (c <= '+') || (c <= '9' && c >= '0') ||
           (c == '.') || (c == '-');
}

float stof(register char *s, uint8_t base)
{
    uint8_t sign;
    char c;
    unsigned dec;
    float  v;

    sign = 0;
    v = 0;
    dec = 1;
    c = '\0';

    while(isspace(*s)) s++;

    switch (*s)
    {
        case '-': sign++;
                  s++;
                  break;
        case '+': s++;
                  break;
        case '0': s++; /* we can ignore any leading zero's */
                  if ((*s) == 'x')
                  {
                      base = 16;
                      s++;
                  }
    }

    while(IsFDigit(c = *s++))
    {
        c = tolower(c);

        if (c == '.')
        {
            base = 1;
            dec = 10;
        }
        else
        {

            if(isdigit(c)) c -= '0';
            else c -= 'a' - (char)10;

            v = v * (float)base;
            v = v + (float)c/dec;
            if (dec >= 10) dec = dec * 10;
        }
    }

    if (sign != 0) return -v;
    return v;
}

--
Peter Jansen
STS
Australian Antarctic Division

___________________________________________________________________________

   Australian Antarctic Division - Commonwealth of Australia
IMPORTANT: This transmission is intended for the addressee only. If you are not 
the
intended recipient, you are notified that use or dissemination of this 
communication is
strictly prohibited by Commonwealth law. If you have received this transmission 
in error,
please notify the sender immediately by e-mail or by telephoning +61 3 6232 
3209 and
DELETE the message.
       Visit our web site at http://www.aad.gov.au/
___________________________________________________________________________

Reply via email to