Providing implicit conversion of

thinkunix thinkunix at zoho.com
Mon Jan 22 06:43:17 UTC 2024


Gavin Gray via Digitalmars-d-learn wrote:
> The following code:
> 
>    ulong charlie = 11;
>    long johnstone = std.algorithm.comparison.max(0, -charlie);
>    writeln(format!"johnstone %s"(johnstone));
> 
> Results in (without any warning(s)):
> johnstone -11
> 
> However you choose to look at it, this means -11 > 0 (regardless of all 
> arguments concerning implicit conversions, 1's and 2's complements, 
> being efficient, etc).
> 
> The language should not allow unary unsigned anything.
> 

I have no idea what your use case is for this but...
WHY are you doing this??

If you declared charlie as unsigned, why would you then attempt to
compare using a negative value?  If you even had the possibility that
charlie might be negative, why wouldn't you use a type that can 
accomodate the sign?

Using the proper type, you get a proper result:

         long b = 12;
         long n = std.algorithm.comparison.max(0, -b);
         long o = std.algorithm.comparison.max(0, b);
         writeln("n: ", n);              // prints 0
         writeln("o: ", o);              // prints 12

Seems obvious to me, but am I missing something?

scot


More information about the Digitalmars-d-learn mailing list