Part Number: TMS320F2800137
Hi expert,
For this kind of operation, if 16 bits are multiplied by 16 bits and then divided, the temporary result will overflow. Where can the compiler be set to avoid result errors?

Thanks
Joyce
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.
Part Number: TMS320F2800137
Hi expert,
For this kind of operation, if 16 bits are multiplied by 16 bits and then divided, the temporary result will overflow. Where can the compiler be set to avoid result errors?

Thanks
Joyce
Hi Joyce,
if 16 bits are multiplied by 16 bits and then divided, the temporary result will overflow.
This is expected compiler behavior. Please refer to this app note regarding how to write multiplies correctly in C code.
"A common misconception about multiplication is the idea that multiplication takes place in infinite precision, and is then truncated to the result type. This is not so; the multiply will take place in the *narrowest* legal type (according to the types of the operands), and will then be *converted* to the result type, which may be a narrowing or widening conversion."
In order to have the correct result, you will need to make sure you're multiplying by two 32 bit values, you can explicitly declare this.
(uint32_t) lv_ctl.p_val[0] * (uint32_t) cfg->duty / 1000
Best,
Ryan Ma