7

I'm coding in C and I know the values that can be stored in a uint8_t range from 0 to 255. I want to store a value that is the percentage of MAX_INTENSITY, declared as: #define MAX_INTENSITY 200

Can I do the following: uint8_t my_intensity = (MAX_INTENSITY * percentage) / 100; ?

The result will be 200 or less, but during the calculation, the intermediate result can be superior to 255, e.g. with percentage = 50, MAX_INTENSITY * percentage = 10'000. Can this cause a problem, even if the final result will be 100, o in the range of a uint8_t?

ephtelule
  • 91
  • 2
  • 1
    The sub-expression `MAX_INTENSITY * percentage` will be calculated as `int`s throughout. Same as `(MAX_INTENSITY * percentage) / 100`. See [C11 6.3.1.1](https://port70.net/~nsz/c/c11/n1570.html#6.3.1.1) and [C11 6.3.1.8](https://port70.net/~nsz/c/c11/n1570.html#6.3.1.8) – pmg Feb 25 '22 at 09:35
  • Thanks. Would it be the case even if ```percentage``` and ```MAX_INTENSITY``` were variables declared as ```uint8_t``` ? – ephtelule Feb 25 '22 at 09:38
  • Yes, both values will be converted to `int` before the expression is evaluated... and converted back to the smaller type afterwards. – pmg Feb 25 '22 at 09:40
  • I don't really know assembler, but ==> https://godbolt.org/z/76ff8xdv8 <== lines 5,7: full 32-bit operations; lines 12,13: 8-bit operations – pmg Feb 25 '22 at 10:10
  • It is sometimes necessary to explicitly convert operands to a wider integer type to avoid arithmetic overflow during rational scaling operations, but it is not necessary in this case. – Ian Abbott Feb 25 '22 at 10:32

1 Answers1

2

In C, the rules to determine the type used for each intermediary computation are somewhat intricate, but for your particular case quite easy:

The multiplication MAX_INTENSITY * percentage involves an integer constant 200 of type int and a variable percentage. If the type of percentage is int or a smaller type such as uint8_t, this operand will undergo an integer promotion to type int and the result will have type int, then used as an operand for the division by 100, another integer constant with type int, hence a computation with type int and a result of the same type. The result will be converted to the type of the destination, uint8_t, which is performed by take the positive modulo 256, a fully defined operation, especially if percentage is known to be in range 0 to 200.

Therefore, your definition is correct:

uint8_t my_intensity = (MAX_INTENSITY * percentage) / 100;

You might nevertheless consider using type int for this local variable, as there is no benefit in typing it with a smaller type in this context.

chqrlie
  • 131,814
  • 10
  • 121
  • 189