On Sunday, 21 January 2024 at 16:05:40 UTC, Gavin Gray 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.
This returns -1:
import std;
void main() {
ulong charlie = 11;
long johnstone = std.algorithm.comparison.max(0, -charlie);
writeln(format!"johnstone %s"(johnstone));
}
If you change the result type to auto johnstone
, it returns 18446744073709551605:
module app;
import std;
void main() {
ulong charlie = 11;
auto johnstone = std.algorithm.comparison.max(0, -charlie);
writeln(format!"johnstone %s"(johnstone));
}
So what happens is, max() correctly returns 18446744073709551605,
but if you explicitely receive a long
, the ulong
is converted to a long,
resulting in -11.
With auto johnstone
or ulong johnstone
the result is correct:
import std;
void main() {
ulong charlie = 11;
ulong johnstone = std.algorithm.comparison.max(0, -charlie);
writeln(format!"johnstone %s"(johnstone));
}
If you take a bigger type, like Int128
, it is also correct:
import std;
void main() {
ulong charlie = 11;
Int128 johnstone = std.algorithm.comparison.max(0, -charlie);
writeln(format!"johnstone %s"(johnstone));
}