A strange div bug on Linux x86_64, (both dmd & ldc2): long -5000 / size_t 2 = 9223372036854773308

FeepingCreature feepingcreature at gmail.com
Thu Aug 13 10:13:44 UTC 2020


On Thursday, 13 August 2020 at 10:10:34 UTC, H. S. Teoh wrote:
> On Thu, Aug 13, 2020 at 07:22:18AM +0000, mw via Digitalmars-d 
> wrote: [...]
>> void main() {
>>   long   a = -5000;
>>   size_t b = 2;
>>   long   c = a / b;
>>   writeln(c);
>> }
>
> You're mixing signed and unsigned values. That's generally 
> dangerous territory where integer promotion rules inherited 
> from C/C++ take over and cause sometimes weird effects, like 
> here. Changing integer promotion rules will probably never 
> happen now, because it will cause massive *silent* breakage of 
> existing code. So, in the spirit of defensive programming, I 
> recommend avoiding mixing signed/unsigned values in this way.
>
>
> T

Changing integer promotion rules to disallow promotion of signed 
to unsigned for division will not cause massive silent breakage. 
- But it will cause massive visible breakage; Phobos uses this 
all over.


More information about the Digitalmars-d mailing list