4 ms·
I want to add that what the author of the article has correctly noticed, but this was not expressed clearly, is that between any 2 modular number types with dis
by adrian_b 5y ago
I want to add that what the author of the article has correctly noticed, but this was not expressed clearly, is that between any 2 modular number types with distinct moduli there cannot be a subtype relationship.
That means that in languages with modular number types, e.g. C/C++, there should not exist any implicit conversions between modular numbers of different sizes or between a modular integer type and an interval integer type.
That means that all promotions between a smaller unsigned type and a larger unsigned type or between a signed integer type and an unsigned integer type, which exist in C/C++, are wrong and they should be deprecated.
On the other hand the implicit conversions between an interval type and another interval type including it are correct.
So only the implicit integer promotions
signed char => short => int => long => long long
are right in C/C++, all the other implicit integer conversions are wrong.
- gpderetta 5y agoWhile 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.