[OT] The Usual Arithmetic Confusions

Ivan Kazmenko gassa at mail.ru
Sun Jan 30 17:22:18 UTC 2022


On Friday, 28 January 2022 at 18:39:54 UTC, deadalnix wrote:
> On Friday, 28 January 2022 at 02:15:51 UTC, Paul Backus wrote:
>> https://shafik.github.io/c++/2021/12/30/usual_arithmetic_confusions.html
>>
>
> For what it is worth, the first exemple is fixed in D.
>
> The multiplication one is particularly interesting. The other 
> ones really banal wrap around behavior, which your really can't 
> do without if youw ant to make anything fast, IMO.
>
> I'm surprised there aren't more exemple of signed -> unsigned 
> conversion because that one is a real mind fuck.

I wholeheartedly agree with the latter notion.  The dreaded 
signed-to-unsigned conversion has definitely bitten me more than 
once in D.  And not only conversion.  Writing something innocent 
like `auto var = arr.length`, because `auto` types are generally 
more geared towards later code changes.  Then doing arithmetic 
with `var`... one subtraction, and boom! getting the overflow all 
of a sudden.

At least in 64-bit programs, I don't really see the benefit of 
the sizes being unsigned anymore.  Even C++ has `cont.ssize()` 
now for signed size.

Ivan Kazmenko.



More information about the Digitalmars-d mailing list