Utils::atof problem

Hi, I know that you guys hacked atof because it was resulting in some weird problems on android, but some problems were also introduced with that fix.

If we have a valid atof string that involves exponents such as

“8.232424252151e-05”

utils::atof will return 8.2324242 instead of 0.000008. This happens because we are capping to the first 7 decimal digits so before the std::atof “8.232424252151e-05” becomes “8.2324242”.

I propose we check whether or not the exponent character is present, and if so, we keep the last 4/5 characters. That way before std::atof “8.232424252151e-05” would become “8.2324e-05” and we would get the correct float value.

I ended up going with this

double atof(const char* str)
{
    if (str == nullptr)
    {
        return 0.0;
    }
    
    char buf[MAX_ITOA_BUFFER_SIZE];
    strncpy(buf, str, MAX_ITOA_BUFFER_SIZE);
    
    // strip string, only remain 7 numbers after '.'
    char* dot = strchr(buf, '.');
    char* exp = strchr(buf, 'e');

    if (dot != nullptr && dot - buf + 8 <  MAX_ITOA_BUFFER_SIZE)
    {
        int dotOffset = 4;

        if (exp != nullptr && (exp-dot)>dotOffset) {
            while (*exp != '\0') {
                dot[dotOffset++] = *exp;
                exp++;
            }
            dot[dotOffset] = '\0';
        }
        else {
            dot[8] = '\0';
        }
       
    }
    
    return ::atof(buf);
}

What’s wrong with standard atof?

it has some problems on android: