Hey,
I use a MPC560xB microcontroller and I have found an error of a division computation.
This is an example:
/** Data declaration */
signed int s32A;
unsigned int u32B;
signed int s32Result;
/** Data computation */
s32A = -30;
u32B = 30;
s32Result = s32A / s32B
Value for s32Result using debugger is 143165575. This is not the expected value (expected value is -1).
The computation [unsigned value] / [unsigned value] gives the expected result,
the computation [signed value] / [signed value ] gives the expected result
the computation [signed value] / [unsigned value] or [unsigned value] / [signed value] gives a unexpected result (choosen type by the compiler is unsigned instead of signed).
Thanks for your help.
Romain BENET
Hi Romain,
the result of your code is correct. The "problem" is implicit conversion. While the division is executed, the priority of data type is applied (because data types are different) and because signed int has the lowest priority, both operands are implicitly converted to unsigned int according to s32B variable.
Now look, how the computation looks like:
s32A is converted to 42 949 672 266 (max range of unsigned 32-bit int minus 30......2^32 - 30)
s32B is still 30
s32Result = 42 949 672 266 / 30 = 143 165 575,53 (fractional part is removed from result, because the variable is int)
and this is the result you got.
So the solution is to use the same data types, or use explicit conversion during the division, for example:
s32Result = s32A / (signed int)u32B;
Regards,
Martin