8 years ago.

I have problem with floating point calculation

I am working on a code part of ADS1211 AD control, containing a divison which must be floating to be precise. The problem is that in the SetUpdateFreq function I give 10.00 and GetUpdateFreq I read back 101.00 (10.1 would be acceptable by the roundings) and I could not find the problem. Maybe it is in printf in the main file. I do not know, but in integer everything works OK. Could you please spend some time to see the code and advice what can be the problem? Thank you in advance!

int SetUpdateFreq(float fdata) {
    long lresult;
    float fresult;
    unsigned int temp; 
    
    lresult= CRYST_FREQ >> (9 - cmr.CommandBit.turbo); 
    fresult = lresult / fdata;
    temp = fresult-1;
    if ((temp < 19) || (temp > 8000)) return (-1);
    
    cmr.CommandBit.decratl= temp & 0xFF;
    cmr.CommandBit.decrath= temp >> 8;
    WriteCommandRegister();
    return (0);
    }
    
float GetUpdateFreq(void) {
    unsigned int temp;
    long dividend, divider;
    float fresult;
    
    ReadCommandRegister();     
    temp = cmr.CommandBit.decrath<<8 + cmr.CommandBit.decratl;
    dividend = CRYST_FREQ >> (9-cmr.CommandBit.turbo);
    divider = temp+1;
    fresult = dividend / divider;
    return ( fresult );
    }

    sprintf(str," Data Update Freqvency: %.2f\r\n", GetUpdateFreq());
//    sprintf(str," Data Update Freqvency: %i\r\n", GetDecimationRatio());
    PrintString(str); 

Please use <<code>> and <</code>> to make it properly formatted and readable.

posted by Erik - 23 Nov 2016

You say everything works fine in integer. Then you have exactly the same functions only floats replaced by integers?

posted by Erik - 24 Nov 2016

Not exactly. Integer part is temp. I would like to scale the integer constans in frequency. Reading and writing integer to the AD gives the same result. Writing integer and scaling up with floating gives wrong result, basicly 10x of the real figure. Setting the frequency with float gives good integer figure. The problem must be in GetUpdateFreq or sprintf, somehow somewhere there is a 10x "multiplication".

posted by László Garab 24 Nov 2016

Thank you spending some time on me. To answer your question made me to understand better the problem. It is operator precedence issue. << is lower than +, so rather the integer part of the function was wrong and the about 10x multiplication is coming from a random coincidence of the figures. Thanks again teaching me how to publish code in the forum.

posted by László Garab 24 Nov 2016
Be the first to answer this question.