I try to split int from unsigned int and I get unexpected results:
int b; Unsigned int c; Int race; Name res_f; B = -25; C = 5; Res = b / c; // res = 858 993454 res_f = b / c; // res_f = -5.000000 The same thing is OK for '+', '-' and '*', but '/' fails for
PS
This was tested on different compilers and the result was the same.
It is assuming that it is similar or similar (like Objective C), change:
res = b / c; to:
res = b / (int) c; Explanation: B is promoted as a presentation in an unsigned integer, according to the type of promotional rules for mixed expressions. In this process, it overflows from -25 to 0xFFFFFE7 == 42 94967271 . After this, you get an unsigned result of 4294967271 / 5U = 858993454U , which is then being typed back into an integer (there is no overflow in this phase because the result is both signed and unsigned 32 Is in the category of bit ints). By the way, the float result should be the same, within the precise boundaries of a float (I get 858993472.0 ). I wonder if you can get the -5.0 in this case?
Comments
Post a Comment