This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

floating point calculation question

Hello, 

I am using F28377D, and have some question on its floating point calculation. 

I am basically reading ADC values, scale it and output to DAC. For example, I can have 

int16 a, b;   // a, b are 12-bit ADC readings

int32 c; // c is an intermediate variable in the calculation

int16 d; // d is output to DAC

I have some calculation like:

c = (a-10)*50/47; 

d = c+b/7; 

My question is basically what is the best practice for calculation like this? (In my case I care more about speed, it is not a problem if the integer number is off by 1, etc. )

My understanding is that is would be actually better to write

c = (a-10)*1.06382978 

instead of  *50/47, for both accuracy and time? 

Also the intermediate variable c, does it make a speed difference if I declare it as float instead of int32? 

Lastly, is there library that I need to include to use fpu and Trigonometric Math Unit? 

Thank you for clarifying these questions for me.  

  • Hi Shibin,

    *50/47 will not produce wanted results as the compiler will treat 50 and 47 as integers, and being smart it will calculate the result at compile time, which is 1.

    if you would write like this

    c =( (a-10)*50)/47, then you will get wanted result.

    As for what is faster I would ask you how fast do you need it to be? If you are not struggling for time then it makes no sense in optimizing. Go full floating point (e.g *50.0/47.0 and /7.0)

    If you are struggling, then get rid of the divisions first. Either by doing it all in floating point. In this case it will be faster if you declare your intermediate value c as float.

    Given that your input and output value are both fixed point, fixed point solution using only multiplies and right shifts would be the fastest solution.

    Regards, 

    Mitja

  • Hello Mitja, 

    Thank you for your clarification. 

    I update these numbers at a rate of 240kHz with sysclk set at 120MHz. I experimented it and division is indeed too slow. Doing it in floating point multiplication works for me.