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

H. S. Teoh hsteoh at quickfur.ath.cx
Thu Aug 13 23:28:43 UTC 2020


On Thu, Aug 13, 2020 at 11:07:23PM +0000, mw via Digitalmars-d wrote:
[...]
> C#
> ----------------------------------------------------------------------------
[...]
> div.cs(6,14): error CS0019: Operator `/' cannot be applied to operands of
> type `long' and `ulong'
[...]
> 
> Rust:
[...]
> error[E0277]: cannot divide `i64` by `u64`
[...]

Honestly, I'd be happy if we turned these implicit sign conversions to
errors.  The cases where you *want* a/b to convert to unsigned are
limited; if you really want to do it, you could just write a cast.  It
does make the code much clearer:

	ulong x = ...;
	long y = ...;
	auto z = x / cast(ulong) y; // see? now it's completely clear

And before somebody tells me this is too verbose: we already have to do
this for short ints, no thanks to the recent change that arithmetic
involving anything smaller than int will implicitly promote to int
first:

	ubyte x;
	ubyte y;
	//ubyte z = x + y;	// NG
	ubyte z = cast(ubyte)(x + y); // OK

Yes, it's *that* ugly.  Welcome to the Dungeon of D's Dark Corners,
where you see the ugly side of D that people don't want to talk about.
We hope you enjoy your stay. (Or not.) :-D


T

-- 
Caffeine underflow. Brain dumped.


More information about the Digitalmars-d mailing list