3 ms·
> adding two int16_t together to get a int16_t in return is really hard Because the first thing people will do is: int16_t a, b; a = (int16_t) ((b * 1
by p0nce 5y ago
> adding two int16_t together to get a int16_t in return is really hard
Because the first thing people will do is:
int16_t a, b;
a = (int16_t) ((b * 157) >> 12);
and then if b * 157 doesn't promote to 32-bit it overflows in the negative and then the code is incorrect.
- dgrunwald 5y agoYou're assuming `int` is 32-bits. The fact that C implicitly promotes to `int` makes it extremely hard to use the stdint.h typedefs without accidentally making platform-specific assumptions. uint16_t a, b, c; a = 50000; b = 40000; c = a * b; This simple code may or may not have undefined behavior depending on the target platform. (it's fine for 16-bit int, UB for 32-bit int, and fine again for 64-bit int) Numeric promotion means that it's almost impossible to write code that is correct on multiple platforms with different sizeof(int).
- p0nce 5y agoWell there is the integer promotion issue, and the integers-have-unknown-size issue.