4 ms·
While uint32_t is not an uint64_t in the is-a sense, I don't see why any specific value of uint32_t can't be converted to an uint64_t. What kind of error would
by gpderetta 5y ago
While uint32_t is not an uint64_t in the is-a sense, I don't see why any specific value of uint32_t can't be converted to an uint64_t. What kind of error would be prevented by disallowing this conversion?
- Someone 5y agoI think what’s meant is this: if you do uint32_t foo = 42 you’re effectively saying foo = 42 (mod 2³²) So, foo stands for the equivalence set {…, 42-2³², 42, 42+2³², …} If you say uint64_t bar = 42 you’re making the different statement that bar = 42 (mod 2⁶⁴) You can’t say that foo and bar are the same number, because (for example) foo * 2³² ≠ bar * 2³²
- adrian_b 5y agoFor example, if you have an uint8_t with the value 200, that denotes either the number 200 or the number 456 or the number 712 and so on. If you have an uint16_t with the value 200, that denotes either the number 200 or the number 65736 or the number 131272 and so on. So it is obvious that the two "200" do not mean the same thing. If you do a longer sequence of operations with uint8_t or uint16_t, inserting an implicit integer promotion in the middle of the chain of operations will change the end result, depending on the place where it is inserted. (E.g. 200*200 will be either 144 or 40000, depending on whether the promotion is done before or after the multiplication) Of course most programmers do not actually use the "unsigned" numbers as modular numbers but as a surrogate for the lack of proper "unsigned" interval numbers and they attempt to avoid any operations which will cause modular reduction. If the programmer knows that no modular reduction has occurred, he/she can write an explicit type conversion from a smaller "unsigned" type to a larger type. The compiler cannot know which was the intention of the programmer. Because the "unsigned" numbers are defined as modular numbers, not as interval numbers, any implicit conversion can be erroneous. If the conversions are actually correct, the programmer should write them explicitly.