mercredi 8 septembre 2021

Random values popping up on typecasting to datatype

template <class T>
    BOOST_FORCEINLINE bool Decoder::readDecimal(const char* data_ptr, T& input, int &offset, int &size)
    {
        log_.writeln("readDecimal : ", std::string(data_ptr + offset, data_ptr + offset + 10));
        if(offset >= size)
            return false;
        T temp = 0;
        bool decimal = false;
        int mul = 1;
        assert(data_ptr[offset] != '\x01' && data_ptr[offset] != '=');
        for(int i = offset; i < size; i++)
        {
            log_.writeln("readDecimal : ", temp);
            if(data_ptr[i] == '\x01' || data_ptr[i] == '=')
            {
                log_.writeln("readDecimal : ", temp);
                input = temp;
                offset = i + 1;
                return true;
            }
            else if(data_ptr[i] == '.')
            {
                decimal = true;
                continue;
             }
             if(!decimal)
                temp = temp*10 + int(data_ptr[i] - '0');
             else
             {
                log_.writeln("readDecimal : ", temp, " ", int(data_ptr[i] - '0'), " ", T(int(data_ptr[i] - '0')), " ", pow(10,mul), " ", (T(int(data_ptr[i] - '0'))/pow(10,mul)));
                temp = temp + T(int(data_ptr[i] - '0'))/pow(10,mul);
                 mul++;
             }
        }
        return false;
     }

I am using the above function is convert a formatted string to decimal datatypes(float/double), but for some reason, I am seeing some random values popping up at the end of the decimal places.

The output log:

readDecimal : 0.65^A64=20
readDecimal : 0
readDecimal : 0
readDecimal : 0
readDecimal : 0 6 6 10 0.6
readDecimal : 0.6000000238
readDecimal : 0.6000000238 5 5 100 0.05
readDecimal : 0.6500000358
readDecimal : 0.6500000358

Function call:

decoder.readDecimal<float>(data_ptr, MDIncGrp[index].MDEntryPx, offset, data_size);

Aucun commentaire:

Enregistrer un commentaire